首页 新闻 会员 周边 捐助

LINQ 多表 LEFT JOIN + GROUP BY

0
[已解决问题] 解决于 2023-06-03 09:55

var list1 = new List<Person>
{
new Person { Id = 1, Name = "A", Age = 15 },
new Person { Id = 2, Name = "B", Age = 25 },
new Person { Id = 3, Name = "C", Age = 35 },
};

var list2 = new List<Address>
{
new Address { AddressId = 11, PersonId = 1, City = "BJ", Temp1 = 30 },

new Address {AddressId = 12, PersonId = 2, City = "SH" , Temp1 = 30},
new Address { AddressId = 13,PersonId = 2, City = "GZ" , Temp1 = 30},

new Address {AddressId = 14, PersonId = 3, City = "SZ", Temp1 = 30 },
new Address {AddressId = 15,  PersonId = 3, City = "CD", Temp1 = 30 },
new Address {AddressId = 16, PersonId = 3, City = "CQ" , Temp1 = 30}

};

var list3 = new List<AddressEntry>
{
new AddressEntry { AddressId = 11, State = "Q1", Temp2 = 30 },

new AddressEntry { AddressId = 12,State = "Q1",  Temp2 = 30 },
new AddressEntry { AddressId = 13,State = "Q2",  Temp2 = 30 },
new AddressEntry { AddressId = 13, State = "Q3", Temp2 = 31 },

new AddressEntry { AddressId = 16,State = "Q1",  Temp2 = 32 },

};

linq 语法能一次得到下面这个结果吗:

感觉一次group by是不行的,他需要2个维度去重,一个是Person.id,另个是Person.id+Address.AddressId,以此类推,现在才是3个表关联,如果4,5个表关联需要去重的更多。

LiveCoding的主页 LiveCoding | 小虾三级 | 园豆:502
提问于:2023-06-02 15:08
< >
分享
最佳答案
0

第3列是Age?为什么会有0

    var result = (from person in list1
                  join b in list2 on person.Id equals b.PersonId into bs
                  from address in bs.DefaultIfEmpty()
                  join c in list3 on address.AddressId equals c.AddressId into cs
                  from addressEntry in cs.DefaultIfEmpty()
                  select new
                  {
                      person.Id,
                      person.Name,
                      person.Age,
                      address?.City,
                      address?.Temp1,
                      addressEntry?.State,
                      addressEntry?.Temp2
                  }).ToList();

    foreach (var item in result)
    {
        Console.WriteLine($"{item.Id}, {item.Name}, {item.Age}, {item.City}, {item.Temp1}, {item.State ?? "noarea", 8}, {item.Temp2 ?? 0}");
    }
奖励园豆:5
复制粘贴机器人 | 小虾三级 |园豆:720 | 2023-06-02 15:44

同一个人的AGE 去重,同一个CITY的TEMP1去重

LiveCoding | 园豆:502 (小虾三级) | 2023-06-02 16:20

这样能不能实现

.groupby()
.selectmany(x=>x.groupby())
.select(x=>new {

})

LiveCoding | 园豆:502 (小虾三级) | 2023-06-02 16:23

@LiveCoding: 感觉不行,光靠GroupBy不能一条有值,一条没值吧。

复制粘贴机器人 | 园豆:720 (小虾三级) | 2023-06-02 17:09
其他回答(1)
0

是的,您观察得很准确,要实现多表的LEFT JOIN和GROUP BY,确实需要对多个维度进行去重。在LINQ中,可以使用匿名类型和匿名对象来实现这个目标。

以下是使用LINQ进行多表LEFT JOIN和GROUP BY的示例代码:

csharp
Copy code
var query = from person in list1
join address in list2 on person.Id equals address.PersonId into addressGroup
from address in addressGroup.DefaultIfEmpty()
join entry in list3 on address?.AddressId equals entry.AddressId into entryGroup
from entry in entryGroup.DefaultIfEmpty()
group new { person, address, entry } by new { person.Id, person.Name, person.Age } into groupedData
select new
{
PersonId = groupedData.Key.Id,
PersonName = groupedData.Key.Name,
PersonAge = groupedData.Key.Age,
Cities = groupedData.Select(x => x.address?.City).Distinct().ToList(),
States = groupedData.Select(x => x.entry?.State).Distinct().ToList()
};
在上述代码中,我们首先使用join关键字对list1和list2进行LEFT JOIN,并将结果与list3进行LEFT JOIN。通过使用into子句和from子句,我们可以处理LEFT JOIN的情况。

然后,我们使用group by子句将数据按照person.Id、person.Name和person.Age进行分组。在分组的结果中,我们使用Distinct()方法对address.City和entry.State进行去重。

最后,我们使用select子句创建一个匿名对象,其中包含了分组后的结果。

通过这样的LINQ查询,您可以一次获取到结果,包括每个人的ID、姓名、年龄以及与之关联的城市和州列表。

请注意,在处理LEFT JOIN时,我们使用了DefaultIfEmpty()方法,以确保即使没有匹配的记录,也能生成结果。

希望这个示例能帮助您实现您想要的结果。如果您有任何进一步的问题,请随时提问。

Technologyforgood | 园豆:7688 (大侠五级) | 2023-06-02 20:20

你是用的GPT,我试过了。嘿嘿。

支持(0) 反对(0) LiveCoding | 园豆:502 (小虾三级) | 2023-06-03 08:24
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册