AspNetBoilerplate 返回所有记录,尽管有 where 子句

AspNetBoilerplate Returning all records depite where clause

我在使用 aspnetboilerplate 进行 LINQ 查询时遇到问题。尽管有 where 子句,但它的 return 所有记录。

我想 select 所有 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; }

}

以下查询 returning 所有记录,即使 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 但我得到相同的结果

也许您需要 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();