I have a search box and I have broken the data into words. I now need compare each of these words with several fields in the database. Here is the code I have:
List<string> searchValues = searchString.Split(delimiters, StringSplitOptions.RemoveEmptyEntries).ToList();
using (DatabaseEntities context = new DatabaseEntities())
{
IQueryable<DatabaseType> returnValue =
context.DatabaseType.Where(y =>
y.Field1.Contains(searchValues[0]) ||
y.Field1.Contains(searchValues[0]));
searchValues.Skip(1).ToList().ForEach(x =>
{
returnValue = returnValue.Where(y =>
y.Field1.Contains(x) ||
y.Field2.Contains(x));
});
return returnValue.ToList();
My goal is to build the full query before actually loading the items into memory. However, when I try to run this I get the following error:
System.NotSupportedException: LINQ to Entities does not recognize the method 'System.String get_Item(Int32)' method, and this method cannot be translated into a store expression.
I had thought this type of error meant that the operations I am attempting do not work in the conversion to SQL, but I’m fairly sure I have used Contains as LIKE statements before. Also, all of the information in the database are nvarchars, so those equate to strings in my Entity. Does any one have any idea what the problem is? Is using an IQueryable the best way to do this? I would prefer to lazy load all of these, if possible.
The get_Item(Int32) calls will be the indexing into the list. In your case they’re searchValues[0]. Use: