The Count() method takes a predicate, so you would do something like this:

if(myObjectList.Count(x => x.Order == 1) >= 2)
    // Do something


First GroupBy MyObject.Order and then determine if Any of the groups have more than one member:

bool b = myObjectList.GroupBy(x => x.Order)
                     .Any(g => g.Count() > 1);
// b is true is there are at least two objects with the same Order
// b is false otherwise


Not a pure Linq-To-Objects-Solution, but how about:

var ordersList = new List<Order>(myObjectList.Select(obj => obj.Order);
bool allUnique = ordersList.Count == new HashSet<Order>(ordersList).Count;

One would have to test performance of all the approaches presented here. I'd be careful, otherwise you end up quickly with some slow O(n²) look-ups.


bool hasDuplicates = myObjectList.Count >
    new HashSet<int>(myObjectList.Select(x => x.Order)).Count;


What about Distinct?

bool allUnique = ordersList.Count == ordersList.Select(o => o.Order).Distinct().Count()

Related Articles