在 LINQ-to-Entities 查询中使用应用程序设置中指定的 StringCollection

发布于 2025-01-03 07:50:03 字数 478 浏览 2 评论 0原文

在我的应用程序中,我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

演多会厌 2025-01-10 07:50:03

更短的路径是

myProperties.Settings.Default.CustomersToExclude.Cast<string>().Contains(blah); 

对于集合本质上不支持 LINQ 的任何情况,这都是一个方便的技巧。

A shorter path is

myProperties.Settings.Default.CustomersToExclude.Cast<string>().Contains(blah); 

That's a handy trick for any situation where a collection isn't inherently LINQ-aware.

雨夜星沙 2025-01-10 07:50:03

由于您的问题被标记为 C# 4,请使用 ListStringCollection 很古老),并且您的查询应该可以工作。此外,您还应该在查询之外解析您的列表引用:

List<string> customersToExclude = ..
var ShippedOrders = dbcontext.Orders
                             .Where(s=>(s.Status.Description.Equals("Shipped") 
                                    && !customersToExclude.Contains(s.CustomerCode)));

编辑:

只需将您的客户复制到数组并使用它:

var customerstoExclude = new string[Properties.Settings.Default.CustomersToExclude.Count];
myProperties.Settings.Default.CustomersToExclude.CopyTo(customerstoExclude, 0);

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:

List<string> customersToExclude = ..
var ShippedOrders = dbcontext.Orders
                             .Where(s=>(s.Status.Description.Equals("Shipped") 
                                    && !customersToExclude.Contains(s.CustomerCode)));

Edit:

Just copy your customers to an array and use that:

var customerstoExclude = new string[Properties.Settings.Default.CustomersToExclude.Count];
myProperties.Settings.Default.CustomersToExclude.CopyTo(customerstoExclude, 0);
神经暖 2025-01-10 07:50:03

相关问题对此进行了回答。不过,EF4 显然直接支持 Contains,所以这将是我首选的解决方案...:)

This is answered in a related question. EF4 apparently supports Contains directly, though, so that'd be my prefered solution... :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文