0

我对使用 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 但我得到相同的结果

4

1 回答 1

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();
于 2019-11-19T18:50:42.230 回答