Im having a problem with the a LINQ query using aspnetboilerplate. despite a where clause its return all records.
I want to select all records that have an EnrolResponse.IsComplete = true.
i have three entities
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; }
}
the following query is returning all records, even if the IsComplete is equal to 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();
If breaking the query down to an IQueryable but i get the same result
Maybe you need All() instead of Any()?
If you use Any() you get all records if at least 1 satisfies the condition. If you use All() you get all records if all satisfy the condition
var enroledUsers = await _userRepository.GetAll()
.Where(x => x.EnrollAttempts.All(y=>y.EnrolResponse.IsComplete == true))
.Include(x=>x.EnrollAttempts)
.ThenInclude(x=>x.EnrolResponse)
.ToListAsync();