使用 Sqlite 的 Linq to EF - 无法搜索字符串 (string.contains)
我正在构建一个 winforms 桌面应用程序,应用程序数据库是 Sqlite。 我正在使用 Linq to Entities 和 Sqlite ADO.NET 提供程序。
在我的应用程序中,我需要搜索文本列内的字符串。 示例(“IsNotEmpty”是我的 !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));
}
问题是该查询正在转换为 CHARINDEX SQL 函数,而 Sqlite 不支持它。
当我切换到 IndexOf(string) 函数时(按照此处的建议) 查询也被转换为 CHARINDEX。
当我切换到 IndexOf(string, index) 时,我收到以下错误:
LINQ to Entities 无法识别“Int32 IndexOf(System.String, Int32)”方法,并且此方法无法转换为存储表达式。
有什么想法吗?我应该切换到 Access 吗?
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 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于安全原因,您不能在 LINQ to Entities 中使用 .NET 函数,例如,
IndexOf()
可能会执行许多其他操作,而不仅仅是检查字符串。使用 Entity SQL http://msdn.microsoft.com/en-us/library/ bb738683.aspx 代替。
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.