开发者

Possible uses for Intersect

I have two queries that produce the same output. One is using the Intersect extension method,开发者_StackOverflow the other is a cross join.

There are other ways of also doing the same thing such as a cross join or a normal join, so what else can Intersect be used for?

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)

Output

IEnumerable (2 items)

1, 3

IEnumerable (2 items)

1, 3


IMHO, using intersect makes your intentions much clearer, and thus makes your code more readable. intsA.Intersect(intsB) immediately tells me that you're producing the set intersection; the LINQ expression requires me to parse the complete, complicated expression, just to figure out the same thing.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜