c# - Return true or false based on whether there are duplicates -


when have duplicates in collection, want return true, otherwise, want return false.

i've got following linq query.

var t = in selecteddrivers         group i.value g         g.count() > 1         select g.count() > 1; 

problem though, when there multiple duplicates, return multiple trues, , if there aren't duplicates, returns nothing (should false).

problem though, when there multiple duplicates, return multiple trues, , if there aren't duplicates, returns nothing (should false).

well that's easy solve:

bool hasdupes = t.any(); 

if there multiple trues, that'll true. if there none, it'll false.

but frankly, inclined write own extension method bails when finds first duplicate, rather building set of duplicates , querying set:

static bool hasduplicates<t>(this ienumerable<t> sequence) {     var set = new hashset<t>();     foreach(t item in sequence)     {         if (set.contains(item))             return true;         set.add(item);     }     return false; } 

and say

bool dupes = selecteddrivers.hasduplicates(); 

easy peasy.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -