使用LINQ连接多个表以返回列表

时间:2019-05-04 22:40:14

标签: linq polymorphic-associations

我有一个LINQ语句正在尝试弄清楚-我对此还比较陌生,所以请原谅我的无知。我想返回一个人列表,每个人都有一个兴趣列表。

person(p)表通过p.id = pi.personid联接到personinterest(pi)表

personinterest表联接到interest(i)表,pi.interestid联接到i.id。

    public class Persons
    {
        public int Id { get; set; }
        public string LastName { get; set; }
        public string FirstName { get; set; }
...
        public IList<Interest> PersonInterests { get; set; }
    }

    public class Interest
    {
        public string InterestName { get; set; }
    }

我要返回的类是一个人,每个人的PersonInterests列表填充了0到许多兴趣。我在下面使用的Linq语句正在返回数据,但是每个人记录仅获得一种利益,并且具有多个利益的人正在复制其人数据,如linq语句所示。

            var interests = _db.Interests;

            return (from p in _db.People
                    join i in _db.PersonInterests on p.Id equals i.PersonId
                    join s in _db.Interests on i.InterestId equals s.Id
                    select new Persons{
                        Id = p.Id,
                        FirstName = p.FirstName,
                        LastName = p.LastName,
                        Age = p.Age,
                        Address = p.Address,
                        City = p.City,
                        StateAbbrev = p.StateAbbrev,
                        ZipCode = p.ZipCode,
                        PersonInterests = (from r in interests where r.Id == i.InterestId select r).ToList()
                    }).ToList();

结果:

{"id":1,"lastName":"Alexander","firstName":"Carson","age":23,"address":"123 4th Street","city":"Jamestown","stateAbbrev":"NV","zipCode":"65465","personInterests":[{"id":1,"interestName":"Basketball"}],"photo":null}
{"id":1,"lastName":"Alexander","firstName":"Carson","age":23,"address":"123 4th Street","city":"Jamestown","stateAbbrev":"NV","zipCode":"65465","personInterests":[{"id":2,"interestName":"Camping"}],"photo":null},

相反,我希望数据看起来像这样:

{"id":1,"lastName":"Alexander","firstName":"Carson","age":23,"address":"123 4th Street","city":"Jamestown","stateAbbrev":"NV","zipCode":"65465","personInterests":[{"id":1,"interestName":"Basketball"}, {"id":2,"interestName":"Camping"}],"photo":null}

我为此苦了一段时间了,非常感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

因此,凝视了几个小时之后,我意识到尝试在一个查询中执行此操作是愚蠢的。我将其拆分并使其正常工作。首先检索人员数据,然后为每个人员建立兴趣列表。

|---------------------|------------------|
|      Column A       |      Column B    |
|---------------------|------------------|
|          1          |         AA       |
|---------------------|------------------|
|          1          |         BB       |
|---------------------|------------------|
|          2          |         BB       |
|---------------------|------------------|
|          2          |         CC       |
|---------------------|------------------|
|          3          |         DD       |
|---------------------|------------------|
|          3          |         EE       |
|---------------------|------------------|

答案 1 :(得分:0)

像这样

(from p in _db.People
    select new {
    Id = p.Id,
    FirstName = p.FirstName,
    LastName = p.LastName,
    Age = p.Age,
    Address = p.Address,
    City = p.City,
    StateAbbrev = p.StateAbbrev,
    ZipCode = p.ZipCode,
    Photo = p.Photo,
    Interests = (from i in _db.Interests
                    where i.PersonId == p.Id
                    select i.InterestName).ToList()
}).ToList();