相交的可能用途

Vin*_*cio 2 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)
Run Code Online (Sandbox Code Playgroud)

产量

IEnumerable(2项)
1,3

IEnumerable(2项)
1,3

kev*_*ner 5

恕我直言,使用相交使你的意图更清晰,从而使你的代码更具可读性.intsA.Intersect(intsB)马上告诉我你正在制作集合交集; LINQ表达式要求我解析完整,复杂的表达式,只是为了弄清楚同样的事情.