Reputation: 49
When i declare
int[] a = { 1, 2 ,10,30};
int[] b = { 10, 20, 30 };
var q = from item1 in a
join item2 in b
on item1 equals item2
into g
select g
1)What is actually getting selected into g ? It is difficult to understand the into
keyword. if you give example to explain the keyword "into"
,i will be pleased.
2)
How do the following code actually projected ?
1 from a in db.coll1
2 join b in db.coll2 on a.PK equals b.PK into b_join
3 from b in b_join.DefaultIfEmpty()
4 where
5 b.PK == null
6 select new {
7 a.PK,
8 a.Value,
9 Column1 = (System.Int32?)b.PK,
10 Column2 = b.Value
}
in line 2 we are using "b' to select the item in line 3 also we are using the same b ,does it mean we are overriding the data we selected at line 2 ?
Upvotes: 0
Views: 97
Reputation: 838984
If you use .NET Reflector on your compiled code, you can see that your first query is equivalent to the following:
IEnumerable<IEnumerable<int>> q = a.GroupJoin(b,
item1 => item1,
item2 => item2,
(item1, group) => group);
Note that you are performing a group join, but only returning the groups. Compare this with an ordinary join:
IEnumerable<int> q = a.Join(b,
item1 => item1,
item2 => item2,
(item1, item2) => item2);
This returns the elements in b that matches to each a, if any such element exists.
Upvotes: 0
Reputation: 888047
1) You are doing a group join. g
will be an IGrouping
for each element in a
. g.Key
will be the element from a
, and g
(which implements IEnumerable
) will contain all of the matching elements from b
. To understand this fully, try it out in LINQPad. Also, read the documentation.
2) The into
keyword merges the first b
into b_join
, which will be an IGrouping
as I described above. After that, the first b
is no longer in scope.
Upvotes: 1