the count() method takes a predicate, so you would do something like this:

if(myobjectlist.count(x => x.order == 1) >= 2)
    // do something


not a pure linq-to-objects-solution, but how about:

var orderslist = new list<order>( => 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.


what about distinct?

bool allunique = orderslist.count == => o.order).distinct().count()


bool hasduplicates = myobjectlist.count >
    new hashset<int>( => x.order)).count;


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

Related Query