Sql – LinqToSql Left Outer Join Multi-Conditions

linq-to-sql

In some (or many) ways linqtosql makes its hard to recreate a very easy sql. I've tried googling but couldn't find any answer on how to convert to linqtosql the code below with a left outer join that has one condition that should evaluate a column value to true. Thanks in advance to anyone who can help (preferably in c#).

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

LEFT OUTER JOIN Yield ON Yield.ToStockTypeID = StockType.StockTypeID AND StockType.IsWholeFormForSpecies = 1

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

Best Answer

An example left outer join using multiple conditions, but not your complete query:

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
    };