LinqToSql左外连接多条件

时间:2009-07-17 05:21:35

标签: linq-to-sql

在某些(或许多)方面,linqtosql很难重新创建一个非常简单的sql。我已经尝试使用谷歌搜索,但无法找到任何关于如何使用左外连接转换为linqtosql的答案,左外连接有一个条件应该将列值评估为真。提前感谢任何可以提供帮助的人(最好是c#)。

SELECT * 从
StockType INNER JOIN StoreProduct ON StockType.StockTypeID = StoreProduct.StockTypeID

LEFT OUTER JOIN收益率Yield.ToStockTypeID = StockType.StockTypeID AND St​​ockType.IsWholeFormForSpecies = 1

LEFT OUTER JOIN YieldGrade ON YieldGrade.YieldID = Yield.YieldID AND YieldGrade.SizeGradeCode ='B'

1 个答案:

答案 0 :(得分:2)

使用多个条件但不是完整查询的左外连接示例:

var query = from s in db.StockType 
    join y in db.Yield on 
        new { s.StockTypeID, s.IsWholeFormForSpecies } 
        equals 
        new { StockTypeID = y.ToStockTypeID, IsWholeFormForSpecies = 1 } 
        into y1
    from y2 in y1.DefaultIfEmpty()
    select new 
    {
        StockType = s,
        Yield = y2
    };