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

后端 未结 3 1860
清歌不尽
清歌不尽 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:48

    I agree to author of this post - it is still look like a bug! If you have empty joined tables you always receive "Object reference not set to an instance of an object.". The only way is to check joined tables to null:

        IEnumerable clubServices =
            from s in services
            from c in clubs.Where(club => club.ClubId == s.ClubId).DefaultIfEmpty()
            from t in clubs.Where(tenant => tenant.TenantId == c.TenantId).DefaultIfEmpty()
            select new Models.Service
            {
                ServiceId = s.ServiceId.ToString(),
                ClubId = c == null ? (int?)null : c.ClubId,
                ClubName = c == null ? null : c.Name,
                HasTimeTable = s.HasTimeTable,
                MultipleCount = s.MultipleCount,
                Name = s.Name,
                Tags = s.Tags.Split(';', StringSplitOptions.RemoveEmptyEntries),
                TenantId = t == null ? (int?)null : t.TenantId,
                TenantName = t == null ? null : t.Name
            };
    

    I unable to check "detailText = person.PersonDetails.Select(d => d.DetailText).SingleOrDefault()" because my joined tables are in different DB.

提交回复
热议问题