簡體   English   中英

SQL to Linq多個表左外部聯接

[英]SQL to Linq multiple tables left outer join

我在SQL中有此查詢,並且希望它LINQ using Entity FrameworkLINQ 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語法from var in collection join in使用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
                    };
}

希望對您有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM