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, 3IEnumerable (2 items)
1, 3IMHO, 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.
精彩评论