I am building a winforms desktop application and the app DB is Sqlite. I am using Linq to Entities with the Sqlite ADO.NET provider.
In my app I need to search a string inside a text column. Example ("IsNotEmpty" is my extension method for !string.IsNullOrEmpty(str)):
if (param.FirstName.IsNotEmpty())
{
selectedCustomers = selectedCustomers.Where(c => (c.firstName.Contains(param.FirstName)));
}
if (param.LastName.IsNotEmpty())
{
selectedCustomers = selectedCustomers.Where(c => c.lastName.Contains(param.LastName));
}
The problem 开发者_JS百科is that this query is being translated to CHARINDEX SQL function and Sqlite doesn't support it.
When I am switching to IndexOf(string) function (as suggested here) the query is being translated to CHARINDEX too.
When I am switching to IndexOf(string, index) I receive the following error:
LINQ to Entities does not recognize the method 'Int32 IndexOf(System.String, Int32)' method, and this method cannot be translated into a store expression.
Any idea? Should I switch to Access?
You can't use .NET functions in LINQ to Entities its because of security, e.g. IndexOf()
may do lot of other things than just checking the string.
Use Entity SQL http://msdn.microsoft.com/en-us/library/bb738683.aspx instead.
精彩评论