更改为linq左连接查询

时间:2017-02-02 02:43:23

标签: sql linq linq-to-sql

SELECT I.*, SI.SupplierID FROM item I
LEFT JOIN SupplierItem SI ON I.ItemID = SI.ItemID AND I.Price = SI.Price
WHERE I.CurrentQty <= I.ReorderLevel and SI.SupplierID = 'AlPA'

如何将该SQL查询更改为Linq Query。

1 个答案:

答案 0 :(得分:0)

以下查询将您的Sql查询转换为Linq查询

var data = 
    from row in db.item
    join row1 in db.SupplierItem
        on new { ID= row.ItemID , Price = row.Price } 
        equals new { ID= row1.ItemID , Price = row1.Price } into joinedData
    from row2 in joinedData.DefaultIfEmpty()
    where row.CurrentQty <= row.ReorderLevel && row2.SupplierID == "AlPA"
    select new {
        item = row,
        SupplierID = row2.SupplierID
    };