Net Core 3.1 Entity Framework:获取所有不包含 属性 的记录

Net Core 3.1 Entity Framework: Get all records where a property is NOT included

在我的 Net Core 3.1 项目中,我有以下模型:

public class Team
{
    public int Id {get; set;
    public string TeamName { get; set; }

    public bool IsMainTeam { get; set; }

    // relation with AppUser, the owner of the team
    public string OwnerId { get; set; }
    public AppUser Owner { get; set; }

    // navigational planes for m2m with users
    public ICollection<TeamsAppUsers> Members { get; set; }

}


public class TeamsAppUsers
{
    public Guid AppUserId { get; set; }
    public AppUser Member { get; set; }

    public Guid TeamId { get; set; }
    public Team Team { get; set; }
}

public class AppUser
{
    public int Id{ get; set; }

    public Guid ExtendedAppUserId { get; set; }

    /// <summary>
    /// The relation to the user's extended profile.
    /// </summary>
    public ExtendedAppUser ExtendedProfile {get; set;}

    /// <summary>
    /// Collection of teams this user is a member of.
    /// </summary>
    public ICollection<TeamsAppUsers> TeamsAppusers { get; private set; }

}

我想编写一个查询,其中返回所有团队,其中提供的 userId 不是 OwnerId 且不在成员列表中(因此,TeamAppUsers 中的 AppUserId 不等于提供的 userId)。基本上,所有与用户无关的团队都应该返回...

我有这样的东西,但它不起作用:

var teams = await _context.Teams
    .Include(t => t.Owner)
    .Include(t => t.ExtendedTeamProfile)
    .Include(t => t.Members)
    .Where(t => t.Members.Any(m => m.AppUserId != userGuid) || t.OwnerId != userGuid)                    
    .ToListAsync();

成员过滤属性似乎是这里的问题,欢迎任何帮助!

编辑:显然,正如所指出的,||运算符应为 &&。关闭。

all the teams are returned where the provided userId is not the OwnerId and not in the list of members (emphasis mine)

然而在您的代码中您使用了 ||(即,或)。你不想要这样的东西吗:

var teams = await _context.Teams
    .Include(t => t.Owner)
    .Include(t => t.ExtendedTeamProfile)
    .Where(t => !t.Members.Any(m => m.AppUserId == userGuid) && t.OwnerId != userGuid)                    
    .ToListAsync();

TLDR:一大堆布尔否定问题。