Is it better to have 2 Where clauses or 1 Where clause with && operator or does it not matter?
list.Where(x => x.Prop1 == value1).Where(x => x.Prop2 == value2).ToList();
Or
list.Where(x => x.Prop1 == value1 && x.Prop2 == value2).ToList();
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It’s better to use the second with two tests in a single lambda. It will loop the list only once and call a delegate only half as often. The first version loops the list twice.
Just to be clear, this is the better option:
Which can also be written
If you can avoid the
.ToList()call and use it as anIEnumerable<T>, you’ll usually get even better perf (unless you read it over and over).