相交的可能用途

时间:2010-07-06 02:38:35

标签: c# linq

我有两个产生相同输出的查询。一种是使用Intersect扩展方法,另一种是交叉连接。

还有其他方法可以做同样的事情,比如交叉连接或普通连接,那么Intersect可以用于什么?

int[] intsA = new[] {1,4,7,0,3};
int[] intsB = new[] {2,3,8,9,1};

intsA.Intersect(intsB)

(from a in intsA
 from b in intsB
 where a == b
 select a)

输出

IEnumerable(2项)
1,3,

IEnumerable(2项)
1,3,

1 个答案:

答案 0 :(得分:5)

恕我直言,使用交叉使你的意图更清晰,从而使你的代码更具可读性。 intsA.Intersect(intsB)立刻告诉我你正在制作集合交集; LINQ表达式要求我解析完整,复杂的表达式,只是为了找出相同的东西。