LINQ 中的联接和子查询
我正在尝试与子查询进行连接,但似乎无法得到它。这是在 sql 中工作的样子。我如何开始在 linq 中工作?
SELECT po.*, p.PermissionID
FROM PermissibleObjects po
INNER JOIN PermissibleObjects_Permissions po_p ON (po.PermissibleObjectID = po_p.PermissibleObjectID)
INNER JOIN Permissions p ON (po_p.PermissionID = p.PermissionID)
LEFT OUTER JOIN
(
SELECT u_po.PermissionID, u_po.PermissibleObjectID
FROM Users_PermissibleObjects u_po
WHERE u_po.UserID = '2F160457-7355-4B59-861F-9871A45FD166'
) used ON (p.PermissionID = used.PermissionID AND po.PermissibleObjectID = used.PermissibleObjectID)
WHERE used.PermissionID is null
I am trying to do a join with a sub query and can't seem to get it. Here is what is looks like working in sql. How do I get to to work in linq?
SELECT po.*, p.PermissionID
FROM PermissibleObjects po
INNER JOIN PermissibleObjects_Permissions po_p ON (po.PermissibleObjectID = po_p.PermissibleObjectID)
INNER JOIN Permissions p ON (po_p.PermissionID = p.PermissionID)
LEFT OUTER JOIN
(
SELECT u_po.PermissionID, u_po.PermissibleObjectID
FROM Users_PermissibleObjects u_po
WHERE u_po.UserID = '2F160457-7355-4B59-861F-9871A45FD166'
) used ON (p.PermissionID = used.PermissionID AND po.PermissibleObjectID = used.PermissibleObjectID)
WHERE used.PermissionID is null
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果没有看到您的数据库和数据模型,就几乎不可能提供任何真正的帮助。但是,最好的方法可能是:
所以,基本上,将您的问题分解为更小的部分。 Linqpad 非常棒,因为它可以让您测试这些东西,并随时检查结果
希望这有帮助,祝
托比好运
Without seeing your database and data model, it's pretty impossible to offer any real help. But, probably the best way to go is:
So, basically, split your problem up into smaller pieces. Linqpad is fantastic as it lets you test these things out, and check your results as you go
hope this helps, good luck
Toby
您的查询的 LINQ 翻译非常简单:
换句话说:“从所有对象权限中,检索至少具有一项用户权限且 UserID 为 2F160457-7355-4B59-861F-9871A45FD16 的对象权限”。
您会注意到,此查询使用关联属性来导航关系 - 这避免了“连接”的需要并简化了查询。因此,LINQ 查询比原始 SQL 查询更接近其英文描述。
编写 LINQ 查询时的技巧是改掉将 SQL“音译”为 LINQ 的习惯。
The LINQ translation for your query is suprisingly simple:
In words: "From all object permissions, retrieve those with at least one user-permission whose UserID is 2F160457-7355-4B59-861F-9871A45FD16".
You'll notice that this query uses association properties for navigating relationships - this avoids the need for "joining" and simplfies the query. As a result, the LINQ query is much closer to its description in English than the original SQL query.
The trick, when writing LINQ queries, is to get out of the habit of "transliterating" SQL into LINQ.