Ale*_*lex 2 .net c# linq collections
如果我有2个对象集合,是否可以在一个查询中有效地加入它们并从中选择它们?
例如:
var collection1 = GetSomeData();
var collection2 = GetSomeMoreData();
var myResult = from x in collection1
from y in collection2
where x.SomeProperty = "Yes"
where y.SomeProperty = "Yes"
select x, select y;
Run Code Online (Sandbox Code Playgroud)
GetSomeData,GetSomeMoreData都回归IEnumerable<MyType>
显然这不会编译......但希望它能说明我正在尝试做什么......
我想你想要Enumerable.Concat:
var myResult = collection1.Concat(collection2)
.Where(x => x.SomeProperty == "Yes");
Run Code Online (Sandbox Code Playgroud)
或者在查询语法中:
var myResult = from x in collection1.Concat(collection2)
where x.SomeProperty == "Yes"
select x;
Run Code Online (Sandbox Code Playgroud)
请注意,在LINQ的上下文中,您使用的单词"join"通常是指基于某个键查找(和配对/分组)两个序列之间的公共元素.这不是你打算做的,不是吗?
| 归档时间: |
|
| 查看次数: |
329 次 |
| 最近记录: |