首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Linq的多个外部连接,其中2个连接到同一个表/对象。有SQL,需要Linq to Entity

使用Linq的多个外部连接,其中2个连接到同一个表/对象。有SQL,需要Linq to Entity
EN

Stack Overflow用户
提问于 2012-04-14 01:34:37
回答 1查看 10.2K关注 0票数 2

我正在尝试在Linq中重现以下SQL查询,需要一些帮助。

代码语言:javascript
复制
select
   dbo.Documents.DocId, 
   dbo.Documents.ReykerAccountRef, 
   dbo.Documents.ReykerClientId DocClientID,
   CAAs.ClientId CAAClientIDCheck,
   ClientData.FullName ClientFullName, 
   CAAs.IFAId,  
   AdvisorData.FullName AdvisorFullName
from dbo.Documents
left join 
  dbo.CAAs on dbo.Documents.ReykerAccountRef = dbo.CAAs.AccountRef
left join 
  dbo.hmsProfileDatas AS ClientData
on 
  dbo.CAAs.ClientId = ClientData.ReykerClientID
left join 
  dbo.hmsProfileDatas AS AdvisorData
on 
  dbo.CAAs.IFAId = AdvisorData.ReykerClientID

我尝试链接到同一个表两次,一次用于客户端全名,另一次用于Advisor全名。

我想在linq中生成的基本sql是

代码语言:javascript
复制
select table1.*,table2.*,a.Fullname, b.Fullname
from table1
left join
   table2 on table1.t2Id = table2.Id
left join
   table3 AS a 
on 
   table2.t3Id1 = table3.id1
left join
   table3 AS b 
on 
   table2.t3Id2 = table3.id2

因此,表1连接到table2,并且table2有2个外键(t3Id1和t3Id2)来table3到不同的字段(id1和id2)。

这就是我尝试遵循一些指导的方法,但是它没有返回任何东西!出什么问题了?

代码语言:javascript
复制
        var results3 = from doc in DataContext.Documents
                       from caa
                           in DataContext.CAAs
                           .Where(c => c.AccountRef == doc.ReykerAccountRef)
                           .DefaultIfEmpty()
                       from cpd
                           in DataContext.hmsProfileDatas
                           .Where(pdc => pdc.ReykerClientID == caa.ClientId)
                           .DefaultIfEmpty()
                       from apd
                           in DataContext.hmsProfileDatas
                           .Where(pda => pda.ReykerClientID == caa.IFAId)
                           .DefaultIfEmpty()
                       select new DocumentInList()
                                  {
                                      DocId = doc.DocId,
                                      DocTitle = doc.DocTitle,
                                      ReykerDocumentRef = doc.ReykerDocumentRef,
                                      ReykerAccountRef = doc.ReykerAccountRef,
                                      ClientFullName = cpd.FullName,
                                      AdvisorFullName = apd.FullName,
                                      DocTypeId = doc.DocTypeId,
                                      DocTypes = doc.DocTypes,
                                      DocDate = doc.DocDate,
                                      BlobDocName = doc.BlobDocName,
                                      UploadDate = doc.UploadDate,
                                  };
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-04-14 01:52:13

我希望我正确理解了你的例子。这是另一个简单的例子,它应该给出你需要的东西:

代码语言:javascript
复制
    private class User
    {
        public int UserId;
        public string Name;
        public int GroupId;
        public int CollectionId;
    }

    public class Group
    {
        public int GroupId;
        public string Name;
    }

    public class Collection
    {
        public int CollectionId;
        public string Name;
    }

    static void Main()
    {
        var groups = new[] { 
            new Group { GroupId = 1, Name = "Members" },
            new Group { GroupId = 2, Name = "Administrators" } 
        };
        var collections = new[] { 
            new Collection { CollectionId = 1, Name = "Teenagers" },
            new Collection { CollectionId = 2, Name = "Seniors" } 
        };
        var users = new[] { 
            new User { UserId = 1, Name = "Ivan", GroupId = 1, CollectionId = 1 },
            new User { UserId = 2, Name = "Peter", GroupId = 1, CollectionId = 2 },
            new User { UserId = 3, Name = "Stan", GroupId = 2, CollectionId = 1 },
            new User { UserId = 4, Name = "Dan", GroupId = 2, CollectionId = 2 },
            new User { UserId = 5, Name = "Vlad", GroupId = 5, CollectionId = 2 },
            new User { UserId = 6, Name = "Greg", GroupId = 2, CollectionId = 4 },
            new User { UserId = 6, Name = "Arni", GroupId = 3, CollectionId = 3 },
        };

        var results = from u in users
                      join g in groups on u.GroupId equals g.GroupId into ug
                      from g in ug.DefaultIfEmpty()
                      join c in collections on u.CollectionId equals c.CollectionId into uc
                      from c in uc.DefaultIfEmpty()
                      select new { 
                          UserName = u.Name, 
                          GroupName = g != null ? g.Name : "<No group>",
                          CollectionName = c != null ? c.Name : "<No collection>"
                      };
    }

它在一个表上生成两个连接,以便从其他两个表中获取数据。下面是输出:

代码语言:javascript
复制
Ivan    Members         Teenagers
Peter   Members         Seniors
Stan    Administrators  Teenagers
Dan     Administrators  Seniors
Vlad    <No group>      Seniors
Greg    Administrators  <No collection>
Arni    <No group>      <No collection>
票数 12
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10145562

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档