EntityFrameworkCore - Join, GroupJoin
GroupJoin 的用法
假设有以下两张表
public class A { public int Id { get; set; } public string Content { get; set; } } public class B { public int Id { get; set; } public int AId { get; set; } public string Value { get; set; } }
我们可以说 A 对 B 是一对多的关系
那么我希望查询 A 的时候, 也能获得相对应A的 B的所有集合
那么我可以这样查询
var query = AList .GroupJoin( BList, a => a.Id, b => b.AId, (a, b) => new { Id = a.Id, Content = a.Content, BList = b.Select(s => s.Value).ToList() }) .ToList();
但是这里有个很隐藏的坑, 详情可以见相关的文章日记