C# Linq 选择列表中存在 IN (A,B,C) 的对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14257360/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Linq select objects in list where exists IN (A,B,C)
提问by MartinS
I have a list of orders
.
I want to select orders
based on a set of order statuses.
我有一个列表orders
。
我想orders
根据一组订单状态进行选择。
So essentially select orders where order.StatusCode in ("A", "B", "C")
所以本质上 select orders where order.StatusCode in ("A", "B", "C")
// Filter the orders based on the order status
var filteredOrders = from order in orders.Order
where order.StatusCode.????????("A", "B", "C")
select order;
采纳答案by Tim Schmelter
Your status-codes are also a collection, so use Contains
:
您的状态代码也是一个集合,因此请使用Contains
:
var allowedStatus = new[]{ "A", "B", "C" };
var filteredOrders = orders.Order.Where(o => allowedStatus.Contains(o.StatusCode));
or in query syntax:
或在查询语法中:
var filteredOrders = from order in orders.Order
where allowedStatus.Contains(order.StatusCode)
select order;
回答by Tim Schmelter
var statuses = new[] { "A", "B", "C" };
var filteredOrders = from order in orders.Order
where statuses.Contains(order.StatusCode)
select order;
回答by Soner G?nül
回答by Alexander Christov
NB:this is LINQ to objects, I am not 100% sure if it work in LINQ to entities, and have no time to check it right now. In fact it isn't too difficult to translate it to x in [A, B, C]but you have to check for yourself.
注意:这是 LINQ to objects,我不是 100% 确定它是否在 LINQ to entity 中工作,现在没有时间检查它。事实上,在 [A, B, C] 中将其转换为x并不太难,但您必须自己检查。
So, instead of Containsas a replacement of the ????in your code you can use Anywhich is more LINQ-uish:
所以,而不是包含作为替代???? 在您的代码中,您可以使用Any更像 LINQ:
// Filter the orders based on the order status
var filteredOrders = from order in orders.Order
where new[] { "A", "B", "C" }.Any(s => s == order.StatusCode)
select order;
It's the opposite to what you know from SQL this is why it is not so obvious.
这与您从 SQL 中知道的相反,这就是为什么它不是那么明显。
Of course, if you prefer fluent syntax here it is:
当然,如果你更喜欢这里的流畅语法,它是:
var filteredOrders = orders.Order.Where(order => new[] {"A", "B", "C"}.Any(s => s == order.StatusCode));
Here we again see one of the LINQ surprises (like Joda-speech which puts select at the end). However it is quite logical in this sense that it checks if at least one of the items (that is any) in a list (set, collection) matches a single value.
在这里,我们再次看到 LINQ 的一个惊喜(如 Joda-speech 将 select 放在最后)。然而,从这个意义上说,它检查列表(集合、集合)中的至少一个项目(即any)是否与单个值匹配是非常合乎逻辑的。
回答by Balvinder Singh
Just be careful, .Contains()
will match any substring including the string that you do not expect. For eg. new[] { "A", "B", "AA" }.Contains("A")
will return you both A and AA which you might not want. I have been bitten by it.
请注意,.Contains()
将匹配任何子字符串,包括您不期望的字符串。例如。new[] { "A", "B", "AA" }.Contains("A")
将返回您可能不想要的 A 和 AA。我被它咬了。
.Any()
or .Exists()
is safer choice
.Any()
或者.Exists()
更安全的选择