score:5
i think this should also work;
names.distinct().tolist().foreach( n => console.writeline(n));
score:0
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()));
}
}else{
//no duplicates
}
score:1
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);
score:2
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: http://blog.jordanterrell.com/post/linq-distinct()-does-not-work-as-expected.aspx
score:8
here you go:
string[] names = {"hello", "hello", "stack", "stack", "overflow", "overflow"};
var distinctnames = names.distinct();
foreach(string distinctname in distinctnames)
console.writeline(distinctname);
Source: stackoverflow.com
Related Query
- How to remove duplicates from an Array using LINQ
- How to remove duplicates from collection using IEqualityComparer, LinQ Distinct
- How to remove duplicates from SQLite DB - using ENtity and LINQ
- How to remove duplicates from a List of List using Linq
- How do I remove items from generic list, based on multiple conditions and using linq
- How to remove characters from a string using LINQ
- How to remove duplicate combinations from a List<string> using LINQ
- How to remove substring from all strings in a list in C# using LINQ
- How to extract the most common YEAR from an array of DateTime objects using LINQ
- Remove "NULL rows" from a String array using LINQ
- How to remove an item from ListView using LINQ in C#
- how to remove objects from list by multiple variables using linq
- How to remove from LINQ results, matching elements from array
- How to remove any value from Dictionary<string,List<string>> using LINQ
- Remove duplicates from a List<Object> using LINQ
- Using LINQ and EF, how to remove values from database where not in list of items
- How can I remove duplicate, invalid, child nodes from an XML document using Linq to XML?
- Take max date row from duplicates and remove duplicates in the list using linq
- How to reinsert data from one table onto itself using LINQ in code migration?
- Remove duplicates by field from one table using another using LINQ
- How to delete objects from array using LINQ
- Remove Element From String Array Using LINQ Contains Value
- How to remove items from multidimensional List using LINQ
- How to get missing value from two array in linq considering duplicates
- How can I use linq to remove a certain string from an array while it exists as the first or last element
- how to fetch data from database using linq query for relationship 1:N and N:N (between 3 entity) in asp.net mvc EF code first?
- Remove Duplicates from dropdown using Linq
- How to query database using LINQ to bring data from database based on array of months in ASP.Net MVC 5?
- Using linq how do i remove multiple records from a cross reference table
- Using LINQ to remove elements from a List<T>
More Query from same tag
- Null Reference Exception when attempting to bind dropdownlist inside formview
- LINQ select query
- Linq using Aggregate() for List
- Duplicate Rows when Data Binding with LINQ to Entities
- Linq With Entities List within Distinct List
- Trim string in LINQ C#
- C# Value from another List linked to value
- LINQ to List<KeyValuePair>
- How can I use be generic to result of linq query?
- Linq To Entities Group By Max Date in Join
- C# - How to check if first period of time is during second period of time?
- Conditional Linq Queries in VB.NET
- Conditional IF statement in LINQ statement
- Linq query with multiple joins
- transform sql to linq with join, group-by and where
- LINQ to create int array of sequential numbers
- Server Error in '/' Application.Specified cast is not valid
- Lazy evaluation in async environment throwing exceptions using HashSet
- Merge 2 TSV files in C# code cleanup
- C# how to return multiple query in foreach
- LINQ to XML: reading XML in C#
- I can not make the correct linq request
- LEM2 algorithm implementation in C#
- Associate values between two lists with LINQ?
- C# linq order by and other statements with foreach, is there a performance difference?
- aggregate two objects in a list into one
- how to get this data via linq
- Grouping multiple records in one single record
- How to force my lambda expressions to evaluate early? Fix lambda expression weirdness?
- How do I optimize checking for any added, deleted or updated items on a list with Linq to Objects?