1

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.

5
  • is your main target to satisfy either of the condition or both of them? Commented May 25, 2018 at 5:15
  • 1
    Why do you want to combine them? Your queries are more readable, as it's currently written. IMO! Commented May 25, 2018 at 5:18
  • I think the written combined query is right one. Commented May 25, 2018 at 5:20
  • @vikscool: Yes. both the conditions should satisfy -S.Akbar: if I combine, then I can just have one variable sayinf "isDtoValid" Commented May 25, 2018 at 5:25
  • @SurajRai if both are required then the query that you have written is correct, only if you don't want two separate query results. Commented May 25, 2018 at 5:47

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.