簡體   English   中英

我如何在Linq(C#)中聯接多個文件,其中聯接之一是左聯接

[英]How do I join more than one files, in a Linq (C#) where one of the joins is a left join

我的下面的Sql很好用,但是現在我想加入另一個具有公用密鑰的文件。 我要加入的文件可能有一對多記錄,任何建議

這是我的代碼:

 var regPoints = (from x in db.CertPoints
                                 join y in db.CommunityPts on x.PointId equals y.PointId into z
                                 from t in
                                     (from r in z
                                      where r.CommunityId == id select r).DefaultIfEmpty()
                                 where x.TypeId == 1 
                                 select new Points
                                 {
                                     pointId = x.PointId,
                                     pointsDescription = x.PointDescription,
                                     points = x.Points,
                                     dateApplied = t.DateApplied,
                                     pointsEarned = t.PointsEarned,
                                     pointsPending = t.Pending ? true : false,
                                     pointsApproved = t.Approved ? true : false,
                                     notes = t.Notes

                                 }).AsEnumerable();

新的聯接將是一對多的記錄,其中CommunityPts中的鍵是Id,而我要聯接的文件是帶有外鍵CommnunityPtId的文件鏈接“ CommunityPtsDocs”的列表。 如何將其添加到上面的上述sql語句中?

進行以下修改將有助於完成任務,盡管我更喜歡Fluent語法,因為我認為在實現相同的目的時會更加干凈,盡管我沒有在Select語句中從CommunityPtsDocs中選擇任何列

var regPoints = (from x in CertPoints
                 join y in CommunityPts on x.PointId equals y.PointId
                 join s in CommunityPtsDocs on y.Id equals s.CommnunityPtId into k
                 from t in (from r in k where r.CommunityId == id select r).DefaultIfEmpty()
                 where x.TypeId == 1 
                                 select new Points
                                 {
                                     pointId = x.PointId,
                                     pointsDescription = x.PointDescription,
                                     points = x.Points,
                                     dateApplied = t.DateApplied,
                                     pointsEarned = t.PointsEarned,
                                     pointsPending = t.Pending ? true : false,
                                     pointsApproved = t.Approved ? true : false,
                                     notes = t.Notes

                                 }).AsEnumerable();

有時候,我覺得自己是一位導航屬性的傳播者(幸運的是,我並不是唯一的一個)。

您接受的答案可以,就可以了。 但是,使用任何ORM(例如Entity Framework或LINQ-to-SQL)時,應盡可能避免使用join語句。 這是冗長且容易出錯的。 它會導致重復的代碼,並且很容易錯誤地連接錯誤的屬性。

您的CertPoint類可能具有0..1-n導航屬性CommunityPts (列表),而CommunityPt可能具有1-n導航屬性CommunityPtsDocs (也是列表)。 如果您使用的是LINQ-to-SQL,很可能它們已經存在,但是您不知道它們。 如果先使用Entity Framework代碼,則應自己添加它們。

具有這些導航屬性,您的代碼將如下所示:

from cert in CertPoints
from comm in cert.CommunityPts.DefaultIfEmpty()
from doc in comm.CommunityPtsDocs
where comm.CommunityId == id && cert.TypeId == 1 
select new Points
{
    pointId = cert.PointId,
    pointsDescription = cert.PointDescription,
    points = cert.Points,
    dateApplied = comm.DateApplied,
    pointsEarned = comm.PointsEarned,
    pointsPending = comm.Pending ? true : false,
    pointsApproved = comm.Approved ? true : false,
    notes = comm.Notes,
    something = doc.Something
})

現在,ORM將使用正確的聯接將其轉換為SQL,並且您的代碼看起來更加簡潔(請注意,我也更喜歡有意義的范圍變量名稱)。

暫無
暫無

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

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