【发布时间】:2016-02-21 07:46:06
【问题描述】:
假设我有两个位置列表。 First 包含所有可用位置:
List<Location> locations = new List<Location> {
new Location { id = 1, address = "1 Main St.", selected = false },
...
}
另一个是 my 个位置的列表或数组:
List<int> myLocations = new List<int> { 1, 5, 8 };
(ID 是不可预测的,但我的位置保证是所有位置的子集)。
我想外连接两个列表并获得selected = true 位置where locations.id eq myLocations 的结果。
如果我使用 Join() 或 Zip(),那么我会得到 inner join,换句话说,我会丢失 myLocations 中不存在的元素,如果我使用以下 -
var result = from loc in locations
join my in myLocations
on loc.id equals my into myloc
from m in myloc.DefaultIfEmpty()
select new Location {
id = loc.id, address = loc.address, selected = true
};
然后所有位置被标记为选中;更不用说它看起来过于复杂了。 有没有办法在不循环列表元素的情况下做我想做的事?
【问题讨论】: