Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在Linq(C)中,如何连接多个文件,其中一个连接是左连接_C#_Sql_Entity Framework_Linq - Fatal编程技术网

C# 在Linq(C)中,如何连接多个文件,其中一个连接是左连接

C# 在Linq(C)中,如何连接多个文件,其中一个连接是左连接,c#,sql,entity-framework,linq,C#,Sql,Entity Framework,Linq,下面我的Sql非常有效,但现在我想加入另一个具有公共密钥的文件。我想加入的文件可能有一对多的记录,有什么建议吗 这是我的密码: var regPoints = (from x in db.CertPoints join y in db.CommunityPts on x.PointId equals y.PointId into z from t in

下面我的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语句中

下面的修改将有助于完成任务,尽管我更喜欢流利的语法,因为在我看来,实现同样的功能要干净得多,尽管我在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的实体框架或LINQtoSQL,您应该尽可能避免使用join语句。它冗长而且容易出错。它会导致重复的代码,很容易错误地加入错误的属性

类CertPoint可以具有0..1-n导航属性CommunityPts列表,CommunityPt可以具有1-n导航属性CommunityPtsDocs也可以具有列表。如果您使用的是LINQtoSQL,很可能它们已经存在,但您没有意识到它们。如果您首先使用实体框架代码,您应该自己添加它们

有了这些导航属性,您的代码将如下所示:

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,并且您的代码看起来更清晰。注意,我也更喜欢更有意义的范围变量名。

也许您应该首先考虑添加导航属性,这样您就不需要在LINQ中使用这些联接了。这不是SQL。谢谢。。。我正在尝试一种不同的方法:在CertPoints和CommunityPts之间的初始连接下进行简单连接,类似于:将s连接到db.CommunityPtsDocs中的z.Id等于y.CommnunityPtId到k,这将进一步将您的this语句从z中的r更改为从k中的r。我假设我使用了其他地方没有使用过的变量来避免混淆这很可能会起作用,但我在z.Id上遇到了一个错误。它不象zCan。请更新模型,让我试试,我精通流利的语法,而不是SQL语法。非常感谢,我对LINQ很陌生,所以我还在试验。不过,谢谢你,这确实很有意义。除了现在不能使用t访问communityPts表之外,它还能工作。我想我确实找到了另一种方法。由于我使用的是实体框架,我通过Id在communityPts和communityPtsDoc之间创建了一个关联,因此communityPtsDoc是communityPts模型中的一个ICollection。再次感谢您,您教会了我一些东西,但最终在实体框架中链接文件是解决方案。再次感谢您抽出时间很高兴它确实有帮助,我的代码片段更符合LINQ2对象,当然与EF IQueryable方法会有一些不同我主要使用Linq 2对象,而不是IQueryable,这肯定提供了很多好信息,我们允许像EF这样的提供者管理实体关系并创建内部联接