如何过滤 IList 子查询 EF Core 2.2
How to filter IList subquery EF Core 2.2
我有这些(简化的)模型:
public class Participant {
public Guid Id { get; set; }
public string Name { get; set; }
public Guid TeamId { get; set; }
public Team Team { get; set; }
}
public class Team {
...
}
public class Activity {
public Guid Id { get; set; }
public string Name { get; set; }
public int Status { get; set; }
public IList<ActivityAttendee> ActivityAttendees { get; set; }
}
public class ActivityAttendee {
public Guid Id { get; set; }
public Guid ActivityId { get; set; }
public Guid ParticipantId { get; set; }
public int Status { get; set; }
public Participant Participant { get; set; }
}
参与者的身份是这样的:
Guid? participantId = Guid.TryParse(HttpContext.User.Claims.FirstOrDefault(c => c.Type == "Id")?.Value, out Guid tmp) ? (Guid?)tmp : null;
我这样查询活动:
var activities = await _context.Activities.Include(x => x.ActivityAttendees).ThenInclude(x => x.Participant).Where(x => x.Status == 1).AsNoTracking().ToListAsync();
现在我需要过滤参与者参加的活动。我试过:
var participantActivities = activities.Where(x => x.ActivityAttendees.ParticipantId == participantId);
var participantActivities = activities.Where(x => x.ActivityAttendees.Contains(y => x.ActivityAttendees.Where(z => z.ParticipantId == participantId)));
var participantActivities = activities.Where(x => x.ActivityAttendees.Any(y => y.ParticipantId == participantId))
我怎样才能做到这一点?
在 "normal" SQL 我会做这样的事情:
SELECT Id, Name FROM Activities WHERE Id IN (Select ActivityId FROM ActivityAttendees WHERE ParticipantId = participantId))
接下来,我还需要过滤参与者没有参加但有团队成员参加的活动(按ActivityAttendee.Participant.TeamId分组),但我认为当上述问题得到回答时我可以做到这一点.
正确的方法是:
var participantActivities = activities.Where(x => x.ActivityAttendees.Any(y => y.ParticipantId == participantId));
我有这些(简化的)模型:
public class Participant {
public Guid Id { get; set; }
public string Name { get; set; }
public Guid TeamId { get; set; }
public Team Team { get; set; }
}
public class Team {
...
}
public class Activity {
public Guid Id { get; set; }
public string Name { get; set; }
public int Status { get; set; }
public IList<ActivityAttendee> ActivityAttendees { get; set; }
}
public class ActivityAttendee {
public Guid Id { get; set; }
public Guid ActivityId { get; set; }
public Guid ParticipantId { get; set; }
public int Status { get; set; }
public Participant Participant { get; set; }
}
参与者的身份是这样的:
Guid? participantId = Guid.TryParse(HttpContext.User.Claims.FirstOrDefault(c => c.Type == "Id")?.Value, out Guid tmp) ? (Guid?)tmp : null;
我这样查询活动:
var activities = await _context.Activities.Include(x => x.ActivityAttendees).ThenInclude(x => x.Participant).Where(x => x.Status == 1).AsNoTracking().ToListAsync();
现在我需要过滤参与者参加的活动。我试过:
var participantActivities = activities.Where(x => x.ActivityAttendees.ParticipantId == participantId);
var participantActivities = activities.Where(x => x.ActivityAttendees.Contains(y => x.ActivityAttendees.Where(z => z.ParticipantId == participantId)));
var participantActivities = activities.Where(x => x.ActivityAttendees.Any(y => y.ParticipantId == participantId))
我怎样才能做到这一点?
在 "normal" SQL 我会做这样的事情:
SELECT Id, Name FROM Activities WHERE Id IN (Select ActivityId FROM ActivityAttendees WHERE ParticipantId = participantId))
接下来,我还需要过滤参与者没有参加但有团队成员参加的活动(按ActivityAttendee.Participant.TeamId分组),但我认为当上述问题得到回答时我可以做到这一点.
正确的方法是:
var participantActivities = activities.Where(x => x.ActivityAttendees.Any(y => y.ParticipantId == participantId));