lambda 表达式 - 相当于 sql 外连接
我需要用 C# 编写该方法,该方法将从数据库返回一些特定的字典。假设存储在 2 个表中的数据如下所示:
表 组(id,名称):
1, 'Management'
2, 'IT Department'
3, 'Production'
用户(id,名称,groupId,projectId):
1, 'John', 1, 1
2, 'Ben', 2, 1
现在我需要检查每个项目中每个组都至少有一个用户。获取此信息的 sql 查询将是:
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;
从该查询返回的信息如下所示:
'Management', 1
'IT Department', 1
'Production', 0
该查询有什么特别之处? projectId 的条件包含在“join”部分,而不是“where”部分。因此,“Production”组的行返回值为 0。当我们将 projectId 的条件移至“where part”时,记录将不会出现在返回结果中。
最后一个问题 - 是否可以使用一个 lambda 表达式来实现类似的效果? (我知道我可以实现 2 个集合并使用某种循环语句获得最终结果,但这不是这个问题的主题)
问候,
烧
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,我想是我自己想出来的。生成的 sql 比原始帖子中提供的要复杂一些,但结果集是相同的。
Ok, I think that I figured it out by myself. The sql produced is a little bit more complicated then provided in original post, but the resultset is the same.
LINQ 外连接查询采用以下形式:
如果没有相关的
y
来连接到x
,则从o
选择>g 将为空LINQ Outer Join queries take the form of this:
If there is no related
y
to join to thex
, then theo
selected fromg
will be null半暗环境下拍摄:
Semi shot in the dark: