C# 使用Linq同时查询两个集合
如果我有两个对象集合,是否可以在一个查询中有效地联接它们并从两个对象中进行选择 例如:C# 使用Linq同时查询两个集合,c#,.net,linq,collections,C#,.net,Linq,Collections,如果我有两个对象集合,是否可以在一个查询中有效地联接它们并从两个对象中进行选择 例如: var collection1 = GetSomeData(); var collection2 = GetSomeMoreData(); var myResult = from x in collection1 from y in collection2 where x.SomeProperty = "Yes" wh
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;
GetSomeData
和GetSomeMoreData
都返回IEnumerable
显然,这并没有编译。。。但希望它能说明我的想法……我想你想要:
或在查询语法中:
var myResult = from x in collection1.Concat(collection2)
where x.SomeProperty == "Yes"
select x;
请注意,在LINQ上下文中,您使用的“join”一词通常指基于某个键在两个序列之间查找(并配对/分组)公共元素。这不是你想要做的,是吗?我的结果应该包含什么?也许可以加个例子?谢谢。。。我应该想到string.concat;-)“加入”这个术语使我感到很不自在
var myResult = from x in collection1.Concat(collection2)
where x.SomeProperty == "Yes"
select x;