SQL 到Linq 多表左外连接

SQL to Linq multiple tables left outer join

我在 SQL 中有这个查询,我希望它在 LINQ using Entity Framework 中实现它,但是如何应用多个表的左外连接?

SELECT p.BookMastId as mastId
FROM BookMast p
left outer JOIN (SELECT y.BookMastId as Id, t.VrsnMastId as vrsn FROM BookReceiptMast t  
left outer JOIN BookReceiptDtl y 
on t.BookReceiptMastId = y.BookReceiptMastId) s 
on p.BookMastId = s.Id where s.vrsn = 2

你可以只使用 from var in collection join in 语法,像这样:

using(var cxt = new YourDataBaseContext()){
    var firstJoin = from t in cxt.BookReceiptMast
                    join y in cxt.BookReceiptDtl
                         on t.BookReceiptMastId equals y.BookReceiptMastId
                     into yTemp
                    from y in yTemp.DefaultIfEmpty()
                    select new
                    {
                        Id = y != null ? y.BookMastId : 0,
                        vrsn = t.VrsnMastId
                    };

    var allTables = from p in cxt.BookMast
                    join s in firstJoin
                         on p.BookMastId equals s.Id
                         into sTemp
                    from s in sTemp
                    where s.vrsn == 2
                    select new
                    {
                        mastId = p.BookMastId
                    };
}

希望对你有所帮助