我对使用aspnetboilerplate的LINQ查询有疑问。尽管有where子句,但它返回所有记录。
我想选择所有具有EnrolResponse.IsComplete = true的记录。
我有三个实体
public class User : Entity<int>, IFullAudited
{
public string Email { get; set; }
public List<EnrollAttemptRequest> EnrollAttempts { get; set; }
}
public class EnrollAttemptRequest : Entity<int>
{
public int UserId { get; set; }
public EnrollAttemptResponse EnrolResponse { get; set; }
}
public class EnrollAttemptResponse : Entity<int>, IFullAudited
{
public int EnrollAttemptRequestId { get; set; }
public bool IsComplete { get; set; }
}
以下查询返回所有记录,即使IsComplete等于false。
var enroledUsers = await _userRepository.GetAll()
.Where(x => x.EnrollAttempts.Any(y=>y.EnrolResponse.IsComplete == true))
.Include(x=>x.EnrollAttempts)
.ThenInclude(x=>x.EnrolResponse)
.ToListAsync();
如果将查询细分为IQueryable,但我得到的结果相同
答案 0 :(得分:2)
也许您需要All()而不是Any()吗?
如果使用Any(),则至少有1个满足条件的所有记录。 如果使用All(),则如果满足所有条件,则会获取所有记录
var enroledUsers = await _userRepository.GetAll()
.Where(x => x.EnrollAttempts.All(y=>y.EnrolResponse.IsComplete == true))
.Include(x=>x.EnrollAttempts)
.ThenInclude(x=>x.EnrolResponse)
.ToListAsync();