I am trying to implement a method where the keywords stored in the database for an activity (split by a comma) match the giving string split by a comma.
publ
Yes you can do it like that:
public List<TblActivities> SearchByMultipleKeyword(string keywords)
{
string[] keywordsSeparated = keywords.Split(',');
var results = (from a in Entities.TblActivities
where keywordsSeparated.Any(keyword => a.Keywords.Contains(keyword))
select a).ToList();
return results;
}
Not sure but you can try: Since the error appears to be looking for an array this might work.
string[] keyword = keywords.Split(new char[] {','});
var results = (from a in Entities.TblActivities
where a.Keywords.Split(new char[] {','}).Any(p => keyword.Contains(p))
select a).ToList();
For queries that do not involve too many keywords and too many rows you could implement this simple and quick solution. You can easily get around the Split function by repeatedly refining your results as follows:
public List<TblActivities> SearchByMultipleKeyword(string keywords)
{
string[] keywords = pKeywords.Split(',');
var results = Entities.TblActivities.AsQueryable();
foreach(string k in keywords){
results = from a in results
where a.Keywords.Contains(k)
select a;
}
return results.ToList();
}
String.Split
isn't supported by Entity Framework. This is simply because there is no equivalent in SQL.
A solution is:
[EdmFunction]
attribute like explained here: How to call DB function from EF LINQ query? You cannot do this using the Entity Framework, as the error message says.
However, there are options.
One option is to realize that, if keywords are stored as A,B,C,D
, then x
is in there if
a.Keywords.StartsWith(x + ",") ||
a.Keywords.Contains("," + x + ",") ||
a.Keywords.EndsWith("," + x)
That works if x
does not contain ,
itself. The downside is that this will do a full scan of the table, or of an index containing the Keywords
column.
The other option is to normalize your database. After all, you have a one to many relationship between activity and keyword. Then model it as such: in addition to an Activities
table (without the Keywords column), have a KeyWords
table with two columns, a foreign key to your activities table, and a keyword
column. This will allow you add an index on the keyword
column, which can make the query super-fast.
UPDATE
I reread your question, and noticed that you are not testing for keyword equality, but just Contains
. If so, why don't you just do the following?
a.Keywords.Contains(x)
LINQ to Entities tries to translate your LINQ query into SQL. Since it doesn't know how to do String.Split
in a SQL query, it fails.
This means that unless you want to write a SQL implementation of String.Split
, you can only do it in LINQ to objects, which means you need to load all of your data into memory first, then do the where
clause. One easy way to do this is using .ToList()
:
var results = (from a in Entities.TblActivities select a).ToList(); //Results are now in memory
results = results.Where(a =>
a.Keywords.Split(',').Any(p => keyword.Contains(p))).ToList(); //This uses LINQ-to-objects