Possible Duplicate:
Check if all items are the same in a List
I have a list:
{string, string, string, string}
I need to check if all the items in this list are the same then return true, if not return false.
Can i do this with LINQ?
var allAreSame = list.All(x => x == list.First());
var allAreSame = list.Distinct().Count() == 1;
or a little more optimal
var allAreSame = list.Count == 0 || list.All(x => x == list[0]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With