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

问题描述 投票:0回答:1

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

linq aspnetboilerplate abp
1个回答
0
投票

也许您需要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();
© www.soinside.com 2019 - 2024. All rights reserved.