tags:

views:

48

answers:

1

I have two queries that produce the same output. One is using the Intersect extension method, 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

+5  A: 

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.

kevingessner