ASP.NET Core & EntityFramework Core: Left (Outer) Join in Linq

后端 未结 3 1837
清歌不尽
清歌不尽 2020-12-01 15:58

I am trying to get a left join working in Linq using ASP.NET Core and EntityFramework Core.

Simple situation with two tables:

    Person (id, firstname, lastnam
3条回答
  •  粉色の甜心
    2020-12-01 16:35

    If you need to do the Left joins then you have to use into and DefaultIfEmpty() as shown below.

    var result = from person in _dbContext.Person
                 join detail in _dbContext.PersonDetails on person.Id equals detail.PersonId into Details
                 from m in Details.DefaultIfEmpty()
                   select new
                    {
                        id = person.Id,
                        firstname = person.Firstname,
                        lastname = person.Lastname,
                        detailText = m.DetailText
                    };
    

    You can learn more about it : Left Outer Join in LINQ to Entities

提交回复
热议问题