实体框架 4.1 Linq Contains 和 StartsWith
我正在使用实体框架代码优先。我想根据列表对象从数据库中查询实体。这对于 contains 来说效果很好,但是如何将它与 StartsWith 结合起来呢?
这是我的代码:
List<string> values = new List<string>();
values.Add("A");
values.Add("B");
context.Customer.Where(c => values.Contains(c.Name)).ToList();
我如何查询以 A 或 B 开头的所有客户?
I am using Entity Framework Code First. I want to query entites from database against List objects. This works fine with contains, but how can I combine it with StartsWith?
This is my code:
List<string> values = new List<string>();
values.Add("A");
values.Add("B");
context.Customer.Where(c => values.Contains(c.Name)).ToList();
How can i query against all customers which starts with A or B?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这应该在内存中工作,但我不确定它是否可以通过 EF 转换为 SQL:
This should work in memory, but I am not sure if it could be translated into SQL by EF:
你不需要将它与 StartsWith 结合起来,因为如果它以 A 或 B 开头,那么它显然包含 A 或 B。它不能以 A 或 B 开头而不包含 A 或 B。
所以只需使用 StartsWith 而不是包含。
You don't need to combine it with StartsWith, since if it starts with A or B, then it obviously contains A or B. It can't start with A or B and not contain A or B.
So just use StartsWith instead of Contains.
您可以尝试以这种方式组合这两个函数:
You might try combining the two functions this way: