I am new to Writing Linq Queries and would like to write a query as below.
Brief Info on Requirement:
I need to get a count of distinct group keys for a user with a full join to another table having its extended data
TABLE - 1: Table - 2:
--------------- -------------
| Id | GrpKey | prdId | UserId| | Id | GrpKey | GrpName | UserId
| 1 | 123455 | Test1 | 11111 | 1 | 123455 | GroupOne | 1111
| 2 | 123455 | Test2 | 22222 | 2 | 551234 | GroupTwo | 1111
| 3 | 123455 | Test3 | 22222 | 3 | 233344 |GroupThree| 1111
| 4 | 551234 | Test4 | 11111 | 4 | 278344 |GroupFour | 1111
| 5 | 551234 | Test5 | 11111
| 6 | DBNULL | Test4 | 11111
| 7 | DBNULL | Test5 | 11111
REQD. RESULT for UserId : 11111
--------------------------------
GrpKey | GrpName | Count(GrpKey)
DBNULL | DBNULL | 2
551234 | GroupTwo | 2
123455 | GroupOne | 1
233344 | GroupThree | 0
278344 | GroupFour | 0
Queries Tried:
**LEFT JOIN:**
from item in table1
join grp in table2 on item.GrpKey equals grp.GrpKey into j1
from rt in j1.DefaultIfEmpty()
where item.UserId == "1111"
group rt by rt.GrpKey into g
select new Group
{
UserId = grp.userId
Count = j1.Count(),
Name = grp.GrpName,
Key = grp.GrpKey,
}).ToList();
**RIGHT JOIN:**
from grp in table2
join item in table1 on grp.GrpKey equals item.GrpKey into j1
where grp.UserId == "1111"
group grp by grp.GrpKey into g
select new Group
{
UserId = grp.userId
Count = j1.Count(),
Name = grp.GrpName,
Key = grp.GrpKey,
}).ToList();
result = LeftJoinResult.Union(RightJoinResult).ToList();
Issues With TriedQuery1:
With the above LINQ query, Result set I am getting is:
GrpKey | GrpName | Count(GrpKey)
DBNULL | DBNULL | 2
551234 | GroupTwo | 2
123455 | GroupOne | 1
551234 | GroupTwo | 1
233344 | GroupThree | 1
278344 | GroupFour | 1
Please help me on how to convert this left join to a full join or data as in Reqd result
Thanks in Advance