Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if all of the elements in a list return true for a property using Linq?

Tags:

c#

linq

I would like to have a LINQ statement that calls the property IsValid.
If all elements returned true, I want the statement to return true as well.
How can it be done?

like image 776
the_drow Avatar asked Jun 30 '10 23:06

the_drow


People also ask

How do you check if all items in a list are equal C#?

Using Enumerable. To determine if two lists are equal, where frequency and relative order of the respective elements doesn't matter, use the Enumerable. All method. It returns true if every element of the source sequence satisfy the specified predicate; false, otherwise.

What is any() in c#?

The Any method checks whether any of the element in a sequence satisfy a specific condition or not. If any element satisfy the condition, true is returned.

What is Distinct in linq?

C# Linq Distinct() method removes the duplicate elements from a sequence (list) and returns the distinct elements from a single data source. It comes under the Set operators' category in LINQ query operators, and the method works the same way as the DISTINCT directive in Structured Query Language (SQL).


1 Answers

var allValid = myList.All(item => item.IsValid);
like image 86
Ben M Avatar answered Oct 02 '22 13:10

Ben M