在 EF Core 3.1.1 中加入 3 个表并使用带有 linq 的左外连接

Joining 3 tables and using a left outer join with linq in EF Core 3.1.1

我有 3 个表,NoticesUsersLikes。如果用户喜欢此通知,我想获得所有带有用户名和信息的通知。

到目前为止我有这个代码,但是它returns一个通知多次(每个喜欢一个):

return context.notices
        .GroupJoin(context.Users, notice => notice.CreatedBy, user => user.Id, (notice, users) => new { notice, users })
        .SelectMany(group => group.users.DefaultIfEmpty(), (group, user) =>
        new
        {
            group.notice,
            user
        })
        .GroupJoin(context.Likes, noticeDto => noticeDto.notice.Id, like => like.ItemId, (noticeDto, likes) => new { noticeDto, likes })
        .SelectMany(group => group.likes.DefaultIfEmpty(), (group, like) =>
        new NoticeDto
        {
            CreatedByName = (group.noticeDto.user == null ? "" : group.noticeDto.user.FullName),
            Id = group.noticeDto.notice.Id,
            Liked = like.CreatedBy == userId,
        });

我也试过这个代码..但是我收到一个错误:

return context.notices
                .GroupJoin(context.Users, notice => notice.CreatedBy, user => user.Id, (notice, users) => new { notice, users })
                .SelectMany(group => group.users.DefaultIfEmpty(), (group, user) =>
                new
                {
                    group.notice,
                    user
                })
                .GroupJoin(context.Likes, noticeDto => noticeDto.notice.Id, like => like.ItemId, (noticeDto, likes) => new { noticeDto, likes })
                .Select((group) =>
                new NoticeDto
                {
                    CreatedByName = (group.noticeDto.user == null ? "" : group.noticeDto.user.FullName),
                    Id = group.noticeDto.notice.Id,                 
                    Liked = group.likes != null ? group.likes.Any(w => w.CreatedBy == userId) : false,
                });

这是我得到的错误:

Processing of the LINQ expression 'DbSet .LeftJoin( outer: DbSet .AsQueryable(), inner: notice => notice.CreatedBy, outerKeySelector: user => user.Id, innerKeySelector: (notice, user) => new { notice = notice, user = user }) .GroupJoin( outer: DbSet, inner: noticeDto => noticeDto.notice.Id, outerKeySelector: like => like.ItemId, innerKeySelector: (noticeDto, likes) => new { noticeDto = noticeDto, likes = likes })'
by 'NavigationExpandingExpressionVisitor' failed. This may indicate either a bug or a limitation in EF Core.

任何人都可以帮助我实现我需要的吗?..谢谢。

通知

    public Guid Id { get; set; }
    public Guid CreatedBy { get; set; }

用户

 public Guid Id { get; set; }
 public string FullName{ get; set; }

喜欢

    public Guid Id { get; set; }
    public Guid CreatedBy { get; set; }
    public Guid ItemId { get; set; }

赞的ItemId就是通知的Id? Notice's and Like's CreatedBy is User's Id?

如果是这样,试试这个。

return context.notices.Include(x => x.Users)
                      .Include(x => x.Likes)
                      .Include("Likes.Users");

数据库

结果

已编辑

因为你没有外键和关系,那么你可以试试这个

var users = context.Users;
        
return context.notices.Select(x => new Notice() 
{
     Id = x.Id,
     CreatedBy = x.CreatedBy, 
     Users = users.Where(y => y.Id == x.CreatedBy).FirstOrDefault(), 
     Likes = context.Likes.Where(y => y.ItemId == x.Id)
                    .Select(y => new Likes()
                    {
                        Id = y.Id,
                        CreatedBy = y.CreatedBy,
                        ItemId = y.ItemId,
                        Users = users.Where(z => z.Id == y.CreatedBy).FirstOrDefault()
                    }).ToList()
});