如何转换为Linq

时间:2018-06-23 19:47:28

标签: c# sql linq

如何将以下查询转换为linq

SELECT
    a.ProductId,
    a.Name,
    a.Description,
    b.Quoteid,
    b.Productid,
    b.Quantity,
    b.OriginalPrice
FROM
    Products        AS a
    LEFT JOIN
    QuoteDtails AS b
    ON a.ProductId = b.ProductId
    AND b.QuoteId = 200;

不知道在何处添加AND条件。

感谢和问候

2 个答案:

答案 0 :(得分:1)

如果要编写linq的linq,可以尝试使用此LEFT JOIN,需要添加

  

从[temp collection]中的[Left join talbe collection]中进入[temp collection]。DefaultIfEmpty()

在Linq join

之后

看起来像这样。

from ss in Products
join aa in QuoteDtails
on ss.ProductId equals aa.ProductId into temp
from ds in temp.DefaultIfEmpty()
where ds.QuoteId = 200
select new 
{ 
    ProductId_P = ss.ProductId,
    Name = ss.Name,
    Description = ss.Description,
    Quoteid = ds.Quoteid,
    Productid_Q = ds.Productid,
    Quantity = ds.Quantity,
    OriginalPrice = ds.OriginalPrice
}

答案 1 :(得分:0)

您可以像这样在LINQ查询中添加AND条件:

var res = from p in products
                  join q in quoteDtails on new { Criteria1 = p.ProductID, Criteria2 = 200 } equals new { Criteria1 = q.Productid, Criteria2 = q.Quoteid }
                  select new
                  {
                      ProductId_P = p.ProductID,
                      Name = p.Name,
                      Description = p.Description,
                      Quoteid = q.Quoteid,
                      Productid_Q = q.Productid,
                      Quantity = q.Quantity,
                      OriginalPrice = q.OriginalPrice
                  };