Accepted answer

i think this should also work;

names.distinct().tolist().foreach( n => console.writeline(n));


var duplicates = suppliers_arr
    .groupby(i => i)
    .where(g => g.count() > 1)
    .select(g => g.key); 
if(duplicates.count() > 0){
    foreach (var d in duplicates)
        modelstate.addformerror(string.format("{0} is duplicated",d.tostring()));
     //no duplicates


you can use distinct.

try this:

string[] names = {"hello", "hello", "stack", "stack", "overflow", "overflow"};
var uniquenames = (from c in names select c).distinct();
foreach(string s in uniquenames) console.writeline(uniquenames);


despite the fact that some answer was marked accepted i think i can add some important information about the unexpected problems that you can come across in situations there you have objects in array and you need distinction to be based on some property of that object.

linq's distinct() function may fail to work properly in that situation, so you should use workaround like this(which give you the same result):

var elems = somearray.groupby(x => x.propertytocompare).select(y => y.first());

you can read more here:


here you go:

string[] names = {"hello", "hello", "stack", "stack", "overflow", "overflow"};  
var distinctnames = names.distinct();

foreach(string distinctname in distinctnames) 

Related Query