在 LINQ-to-Entities 查询中使用应用程序设置中指定的 StringCollection
在我的应用程序中,我有 String.Collections.Specialized.StringCollection 类型的属性设置。它包含客户代码列表,例如 MSFT、SOF、IBM 等。我尝试在 where 子句中的 Linq-to-Entities 查询中使用它:
var ShippedOrders = dbcontext.Orders
.Where(s=>(s.Status.Description.Equals("Shipped") && !Properties.Settings.Default.CustomersToExclude.Contains(s.CustomerCode)));
此操作失败,因为 Linq-to-Entities 无法识别 Contains类似于以下内容的消息:
“LINQ-to-Entities 无法识别方法 Contains...”
如何修改上面的代码以避免此错误?
In my application, I have Property Setting which is of type String.Collections.Specialized.StringCollection. It contains a list of customer codes such as MSFT, SOF, IBM etc. I'm trying to use this in a Linq-to-Entities query in the where clause:
var ShippedOrders = dbcontext.Orders
.Where(s=>(s.Status.Description.Equals("Shipped") && !Properties.Settings.Default.CustomersToExclude.Contains(s.CustomerCode)));
This fails as Contains is not recognized by Linq-to-Entities with a message similar to:
"LINQ-to-Entities does not recognize the method Contains...."
How do I revise the code above to avoid this error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
更短的路径是
对于集合本质上不支持 LINQ 的任何情况,这都是一个方便的技巧。
A shorter path is
That's a handy trick for any situation where a collection isn't inherently LINQ-aware.
由于您的问题被标记为 C# 4,请使用
List
(StringCollection
很古老),并且您的查询应该可以工作。此外,您还应该在查询之外解析您的列表引用:编辑:
只需将您的客户复制到数组并使用它:
Since your question is tagged as C# 4 use a
List<string>
instead (StringCollection
is ancient) and your query should work. Also you should resolve your list reference outside your query:Edit:
Just copy your customers to an array and use that:
相关问题对此进行了回答。不过,EF4 显然直接支持 Contains,所以这将是我首选的解决方案...:)
This is answered in a related question. EF4 apparently supports Contains directly, though, so that'd be my prefered solution... :)