C#Linq加入AND条件

时间:2018-06-01 10:55:48

标签: c# sql linq linq-to-sql ssms

我想了解如何在AND声明中加入Linq2SQL条件

所以我的SQL看起来像SSMS

SELECT TBL1.Field1, TBL2.Field2 FROM TABLE1 AS TBL1
INNER JOIN TABLE2 AS TBL2 ON TBL1.TBL2ID = TBL2.ID 
INNER JOIN TABLE3 AS TBL3 ON TBL1.TBL3ID = TBL3.ID AND TBL3.SQID = 20
WHERE TBL1.ID = 3

我的LINQ如下所示

var linq = (from tbl1 in Table1
            join tbl2 in Table2 on tbl1.tabl2ID equals tbl2.ID
            join tbl3 in Table3 on tbl1.tabl3ID equals tbl3.ID
            where tbl1.ID = 3
            select new { field1 = tbl1.field1, field2 = tbl2.field2 
            }).ToList();

那么我如何在AND的第二次加入中使用tbl3?有什么建议吗?

1 个答案:

答案 0 :(得分:0)

只需添加额外的where子句。 tbl3.SQID == 20

var linq = (from tbl1 in Table1
            join tbl2 in Table2 on tbl1.tabl2ID equals tbl2.ID
            join tbl3 in Table3 on tbl1.tabl3ID equals tbl3.ID
            where tbl1.ID == 3 && tbl3.SQID == 20
            select new { field1 = tbl1.field1, field2 = tbl2.field2 
            }).ToList();