Suraj Rai
Suraj Rai

Reputation: 49

combining two lambda expression query

I have the follwing two lambda expression. Can you please help me how can I combine them into one.

private bool CheckForNonUniqueRollNumbers(IEnumerable<EmployeeInfoDto> empDtos)
{

    var dtosToCheck = empDtos.Where(dto => dto.ExceptionIfAny == null).ToList();
    var allNumbersEmpty = empDtos.All(dto => dto.Identity.RollNumber == "");
    if (dtosToCheck.Any() && !allSerialNumbersEmpty)
    {

    }

I want to combine above two queries

I tried the following query, but I am not sure if this is correct

var dtosToCheck = empDtos.Where(dto => dto.ExceptionIfAny == null 
    || dto.Identity.RollNumber == "").ToList();
    if (dtosToCheck.Any())
     {

     }

Thanks.

Upvotes: 1

Views: 125

Answers (1)

Tatranskymedved
Tatranskymedved

Reputation: 4371

If both conditions should be satisfied (together at same time), You should use && to logically connect them (logical AND):

var allNumbersEmpty = empDtos
                            .Where(dto => dto.ExceptionIfAny == null
                                   && dto.Identity.RollNumber == "")
                            .ToList();

Your query with || will make a logical OR, which will apply if one or the other condition applies.

Upvotes: 1

Related Questions