Hello.
I need to write the method in C# that will return some specific dictionary from DB. Lets say that data stored in 2 tables looks like:
table Groups (id, name):
1, 'Management'
2, 'IT Department'
3, 'Production'
The Users (id, name, groupId, projectId):
1, 'John', 1, 1
2, 'Ben', 2, 1
Now I need to check that in every project every group have no less than one user. The sql query to achieve this information will be:
declare @projectId int;
set @projectId = 1;
select g.Name, case when(isnull(u.id,0) > 0) then 1 else 0 end HasUsers
from groups g
left join users u
on g.id = u.groupId and u.projectId = @projectId;
The information that are returned from this query looks like bellow:
'Management', 1
'IT Department', 1
'Production', 0
What's so special in the query? The condition for projectId is included in 'join' part, not 'where' part. In consequence the row for 'Production' group is returned with value 0. When we move condition for projectId to 'where part' the record will not be present in the return result.
Finally the question - is it possible to achieve similar effect using one lambda expression? (I know that I can achieve 2 collections and using some kind of loop statement get the final result, but it's not a subject of this question)
regards,
yaki