score:5
I think this should also work;
names.Distinct().ToList().ForEach( n => Console.WriteLine(n));
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);
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: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
}
Source: stackoverflow.com
Related Articles
- How to remove duplicates from an Array using LINQ
- How to remove duplicates from collection using IEqualityComparer, LinQ Distinct
- Remove "NULL rows" from a String array using LINQ
- Remove duplicates from a List<Object> using LINQ
- Take max date row from duplicates and remove duplicates in the list using linq
- How to remove duplicates from SQLite DB - using ENtity and LINQ
- Remove duplicates by field from one table using another using LINQ
- Remove Element From String Array Using LINQ Contains Value
- Remove Duplicates from dropdown using Linq
- How to remove duplicates from a List of List using Linq
- Using LINQ to remove elements from a List<T>
- Remove duplicates in the list using linq
- Remove instances from a list by using LINQ or Lambda?
- Take the first five elements and the last five elements from an array by one query using LINQ
- Remove items from list that intersect on property using Linq
- Using Linq to remove from set where key exists in other set?
- Remove duplicates while merging lists using Union in LINQ
- linq remove item from object array where property equals value
- How do I remove items from generic list, based on multiple conditions and using linq
- Remove item from list using linq
- How to remove characters from a string using LINQ
- Using LINQ To Query Int Ids From An Array
- Remove a specific item from a list using LINQ
- C# Using Linq to get column from jagged array
- Using LINQ to delete an element from a ObservableCollection Source
- Using LINQ to Extract Specific Values from Multi-Dimensional Array
- Using LINQ to remove duplicates in dictionary and the count of those duplicates
- Explanation of Code : retrieve item from Array using FirstorDefault()
- How to remove duplicate combinations from a List<string> using LINQ
- Check if a value from one array exists in another array using linq
- Custom Func<> delegate inside Linq where clause
- Unable to add records to list passed as object to method
- Linq for filtering data into object with multiple fields, one of them being List<string>
- Filtering a List with another List Inclusive
- Get only one (last) record in one-to-many join with linq-to-entities
- How to build an empty generic IMongoQueryable<TEntity> type queryable object
- C# LINQ & adding databases after deployment
- How to calculate sum of amount for each 5 weeks before in linq?
- Entity Framework Include syntax
- XML invalid Character !.
- Efficient way to remove all entries in a dictionary lower than a specified value
- Ling Query on DataTable with single dynamic Where Clause
- How do I flatten a List<IList>()?
- LINQ Query across Associations
- Entity framework with Include method
- LINQ: From a list of type T, retrieve only objects of a certain subclass S
- In LINQ to SQL, how do you pass parts of a LINQ query into a function
- How to order list of string based on StatrtWith using LINQ
- Searching a tree using LINQ
- Linq expression .Any(List<string>)