具有多个左连接的 Linq 查询

发布于 2024-10-30 13:17:17 字数 1425 浏览 1 评论 0原文

我对 Linq 很陌生,但我必须将几个复杂的 SQL 查询转换为 Linq,所以我买了一份 Linqer。不幸的是,我知道有一个 SQL 查询 Linqer 无法转换为 Linq,因为 Linqer 无法使用子联接转换 SQL。我希望有人可以帮助我处理此 SQL 查询的 Linq 代码:(

SELECT v.*, rci.CustomId, ci.EntryTime As CheckInTime, ci.Operator As CheckInOperator, 
    cis.Name As CheckInStation, co.EntryTime As CheckOutTime, co.Operator As CheckOutOperator, 
    cos.Name As CheckOutStation, cat.Name As Category, clr.Name As Clearance, r.ReasonForVisit As Reason, 
    s.SiteId + ' -- ' + s.SiteName As Site, e.LastName + ', ' + e.FirstName As Employee 
    FROM ((((((((((Visitor v LEFT JOIN VisitorEntry ci ON v.CheckInId = ci.Id) 
    LEFT JOIN VisitorEntry co ON v.CheckOutId = co.Id) 
    LEFT JOIN Station cis ON ci.StationId = cis.Id) 
    LEFT JOIN Station cos ON co.StationId = cos.Id) 
    LEFT JOIN Category cat ON v.CategoryId = cat.Id) 
    LEFT JOIN Clearance clr ON v.ClearanceId = clr.Id) 
    LEFT JOIN Reason r ON v.ReasonId = r.Id) 
    LEFT JOIN Site s ON v.SiteId = s.Id) 
    LEFT JOIN Employee e ON v.EmployeeId = e.Id) 
    LEFT JOIN RecordCustomId rci ON v.Id = rci.ParentId) 
    WHERE 1=1 

我有几个与此类似的 SQL 查询,我需要将其转换为 Linq,这就是为什么我在查询中使用“WHERE”占位符。

)我一直在看这篇博客文章,但我仍然无法理解如何翻译我的查询: http://codingsense.wordpress.com/ 2009/06/16/multiple-list-left-join-in-linq/

谢谢! 麦克风

I'm very novice with Linq but I have to convert several complicated SQL queries into Linq so I bought a copy of Linqer. Unfortunately I know have a SQL query Linqer can't convert to Linq because Linqer cannot convert SQL with sub joins. I am hoping someone can help me with the Linq code for this SQL query:

SELECT v.*, rci.CustomId, ci.EntryTime As CheckInTime, ci.Operator As CheckInOperator, 
    cis.Name As CheckInStation, co.EntryTime As CheckOutTime, co.Operator As CheckOutOperator, 
    cos.Name As CheckOutStation, cat.Name As Category, clr.Name As Clearance, r.ReasonForVisit As Reason, 
    s.SiteId + ' -- ' + s.SiteName As Site, e.LastName + ', ' + e.FirstName As Employee 
    FROM ((((((((((Visitor v LEFT JOIN VisitorEntry ci ON v.CheckInId = ci.Id) 
    LEFT JOIN VisitorEntry co ON v.CheckOutId = co.Id) 
    LEFT JOIN Station cis ON ci.StationId = cis.Id) 
    LEFT JOIN Station cos ON co.StationId = cos.Id) 
    LEFT JOIN Category cat ON v.CategoryId = cat.Id) 
    LEFT JOIN Clearance clr ON v.ClearanceId = clr.Id) 
    LEFT JOIN Reason r ON v.ReasonId = r.Id) 
    LEFT JOIN Site s ON v.SiteId = s.Id) 
    LEFT JOIN Employee e ON v.EmployeeId = e.Id) 
    LEFT JOIN RecordCustomId rci ON v.Id = rci.ParentId) 
    WHERE 1=1 

(I have several SQL queries similar to this one that I need to convert into Linq so that is why I have the "WHERE" placeholder in the query.)

Also I've been looking at this blog post but I'm still having trouble understanding how to translate my query:
http://codingsense.wordpress.com/2009/06/16/multiple-list-left-join-in-linq/

Thanks!
Mike

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

分开我的手 2024-11-06 13:17:17

SQL 中所有这些括号的意义是什么,它们似乎没有做任何事情,如果删除它们,您不会得到完全相同的结果吗?您可以使用 DefaultIfEmpty 方法在 linq 中执行左连接。我猜你正在使用 LinqToSql?下面是我最近从事的一个项目的示例。

return from app in pi_GetApplications()
       from names in app.tContact.tNames // Inner Join
       from addr in app.tContact.tAddresses.DefaultIfEmpty() // Left Outer Join
       select app;

这假设您在 DBML 文件中定义了外键的关联,以便 LinqtoSql 知道实体是如何相关的。

What is the point of all those brackets in your SQL, they don't seem to be doing anything, do you not get the exact same result if they are removed? You use the DefaultIfEmpty method to do left joins in linq. I presume you are using LinqToSql? Below in an example from a recent project i have worked on

return from app in pi_GetApplications()
       from names in app.tContact.tNames // Inner Join
       from addr in app.tContact.tAddresses.DefaultIfEmpty() // Left Outer Join
       select app;

This assumes you have the associations for the foreign keys defined in your DBML file so that LinqtoSql knows how the entities are related.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文