Convert SQL Query to Linq (contains left joins)

后端 未结 1 1202
自闭症患者
自闭症患者 2021-01-07 08:41

I have a query that works perfectly in SQL, but I\'m having the damnedest time converting it to linq. The table (Table1 below) holds status changes for multiple record types

相关标签:
1条回答
  • 2021-01-07 09:31

    I think a direct translation of your SQL would look like this:

    var q = from ds in table1
            where ds.SubmissionTypeId == 1 || ds.SubmissionTypeId == 2
            from di in table2
            from dr in table2
            where (ds.SubmissionTypeId == 2 && ds.SubmissionId == di.Id)
               || (ds.SubmissionTypeId == 1 && ds.SubmissionId == dr.Id)
            select ds;
    

    However, it seems unlikely that this is what you want. If I may speculate about what your intended logic is, I think you want something more like this:

    var q = from ds in table1
            where (ds.SubmissionTypeId == 2 && table2.Any(di => ds.SubmissionId == di.Id))
               || (ds.SubmissionTypeId == 1 && table3.Any(dr => ds.SubmissionId == dr.Id))
            select ds;
    
    0 讨论(0)
提交回复
热议问题