score:3
Accepted answer
You can group items by the key, and then select what item from the group you want to use as value. I use FirstOrDefault
as an example:
... .Select(c => new SampleClass { Id = c.Location.Name, Name = c.Location.Name })
.GroupBy(c => c.Id)
.Select(group => group.FirstOrDefault())
.ToList()
score:0
Is this what you need: http://sprokhorenko.blogspot.com/2009/11/convenient-distinct.html ?
This is an extension for IEnumerable that allows you to .Distinct() for any field (or even several ones using lambdas), which creates IEqualityComparer for you on the fly.
Source: stackoverflow.com
Related Articles
- Distinct by property of class with LINQ
- Linq code to select one item
- LINQ Source Code Available
- LINQ to XML And Distinct Custom Class
- Tell LINQ Distinct which item to return
- IEqualityComparer and Linq Distinct - Hard Code GetHashCode()
- Linq & C# - Inserting distinct data from one class into another
- creating Linq to sqlite dbml from DbLinq source code
- ASP.NET / LINQ / EF: Async on custom distinct comparer class
- In LINQ how to find a distinct item that is joining 2 tables?
- LINQ distinct on class item?
- How to order list in custom Item class with linq query
- Linq Distinct with a single comparison class (and interface)
- Select latest, distinct item with LINQ
- C# Linq Class with Distinct List
- Using Linq to determine if there is more than 2 distinct items in a class
- source code for LINQ 101 samples
- How to get the Distinct item using linq
- LINQ select first item from multi layer class
- LINQ - Entity framework code first - Grouped results to custom class
- Remove item of List of parent class type through LINQ
- Using LINQ How to access values of List item inside a Class
- Linq Code with Distinct or GroupBy not working
- c# Linq or code to extract groups from a single list of source data
- LINQ - outer join to parent class in EF code first
- LINQ getting distinct records based on an item value
- How to write SQL translateable linq code that groups by one property and returns distinct list
- LINQ adding new item (student) to a list of another type (courses) using a connector class
- Convert string[] to int[] in one line of code using LINQ
- Select distinct using linq
- LINQ result to view
- linq query must return a string
- LINQ Select from two Collections with same length
- Converting SQl statement to Linq in C#
- C# Linq XML Query where multiple elements of same name from a parent node based on a child node value
- How to select mutiple rows from Entity using Linq
- VB.NET filtering/grouping a datatable
- Generating Permutations for n sets in LINQ
- Linq - How to aggregate the results of another query
- EF Core: The LINQ expression could not be translated
- Build query for IQueryable (EF4) from multiple parameters
- How to Select the Token and Index from a Comma Separated String in Linq
- Comparing Arrays LINQ
- Linq conditional group by count
- Two objects being equal not regarded as distinct by LINQ
- Select rows with minumum date in join query
- Linq date format
- Convert SortedList to IOrderedEnumerable
- Using LINQ in a List
- horrible and big LINQ statement optimisation