我正在努力尝试在LINQ或方法语法中编写LINQ语句,基于以下SQL。
SELECT vr.*
FROM VisualReport vr
JOIN VisualReportRoleList vrl ON vr.VisualReportSK = vrl.VisualReportSK
JOIN Role r ON vrl.RoleSK = r.RoleID
JOIN UserRoleList url ON r.RoleID = url.RoleID
JOIN Users u ON url.UserID = url.UserID
WHERE u.ID = 1
我尝试了很多东西,但没有运气。我打算在这里发布一些内容,但它只是让这篇文章变得非常麻烦。
这里的任何人都可以帮我吗?
答案 0 :(得分:0)
加入模式就像这样
Param 1.是要加入的列表,
左侧的Param 2.键(您的呼叫加入的内容),
Param 3.右边的键(你加入的是什么),
Param 4.是结果选择器,您希望如何选择结果。
db.VisualReposts.Join(db.VisualRepostRoleLists,
vr => vr.VisualReportSK,
vrl => vrl.VisualReportSK
(vr, vrl) => new { vr, vrl}).Join(db.Roles,
vrj => vrj.vrl.RoleSK,
r => r.RoleID,
vrj, r => new {vr = vrj.vr,
vrl = vrj.vrl,
role = r} )
//follow the same patter for the rest of the joins.
// Also add your where clause, it might be a better idea to start
//from the users table so you can filter first
但如果一切都是正确的外键,你不需要进行连接就可以访问ICollections。如果您可以或者拥有外键,您可以像这样访问它们
var user = db.Users.Where(u => u.id == 1);
//everything should have an icollection now so you can access via
//user.UserRoleList.Roles ect or what ever you need
答案 1 :(得分:0)
简单的一对一转换。 LinqPad应该将其转换为您的查询的等价物。
var result = (from vr in VisualReport
join vrl in VisualReportRoleList on vr.VisualReportSK equals vrl.VisualReportSK
join r in Role on vrl.RoleSK equals r.RoleID
join url in UserRoleList on vrl.RoleSK equals url.RoleID
join u in Users on url.UserID equals u.UserID
where u.ID == 1
select vr).ToList();