Reputation: 122
I have a List<int>
items. How can I check if all the items are different with a LINQ query?
Upvotes: 2
Views: 153
Reputation: 322
This more beautiful:
var isDifferentData = !int.Equals(CurrentList.Distinct().Count(),CurrentList.Count())
Upvotes: 0
Reputation: 39326
Another way to do it is using a HashSet:
var areDifferent= new HashSet<int>(CurrentList).Count==CurrentList.Count;
Upvotes: 1
Reputation: 18155
Some of the other alternatives are
Option 1
(!list.GroupBy(c => c).Any(c => c.Count()>1))
Option 2
list.GroupBy(c => c).All(c => c.Count() == 1)
Upvotes: 3
Reputation: 122
In the meantime I solved it with:
CurrentList.Distinct().Count() < CurrentList.Count()
Upvotes: 4