Check if all values are equal in a list

c#, lambda, linq

Solution

You can use `GroupBy`:

bool allEqual = orders.GroupBy(o => o.qty).Count() == 1;

or, little bit more efficient but less readable:

bool allEqual = !orders.GroupBy(o => o.qty).Skip(1).Any();

or, definitely more efficient using `Enumerable.All`:

int firstQty = orders.First().qty;  // fyi: throws an exception on an empty sequence
bool allEqual = orders.All(o => o.qty == firstQty); 

Problem

``` Class order { Guid Id; int qty; } ``` Using LINQ expression, how can I verify if the `qty` is the same for all orders in a list? Thanks in advance!

Original source