In my Net Core 3.1 project I have the following model:
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; }
}
I want to write a query where all the teams are returned where the provided userId is not the OwnerId and not in the list of members (so, AppUserId in TeamAppUsers is not equal to the provided userId). Basically, all the teams should be returned where the user has nothing to do with...
What I had is something like this, but it doesn't do the trick:
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();
The filtering on the Members property seems to be the problem here, any help is welcome!
Edit: Obviously, as pointed out, the || operator should be &&. Closed.
CodePudding user response:
all the teams are returned where the provided userId is not the OwnerId and not in the list of members (emphasis mine)
Yet in your code you used ||
(ie, or). Wouldn't you want something like this:
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: a whole bunch of boolean negation issues.