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 Query
- 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
More Query from same tag
- Convert XML with Attributes to Object with LINQ
- How can I return a object with the sum of all objects from a list using Linq?
- Can not use Count() on property of Collection linQ
- C# LINQ GroupBy error Only one expression can be specified in the select list
- flat list to hierarchy
- Is there a way to load a dbset dynamically in EF core?
- Assigning an ID to a ViewBag and giving it a value in MVC
- SQL Searching a data containing '%'
- OrderBy and ThenBy combined with nested Where?
- Generic method to calculate Euclidean Distance using expressions
- Can't find a certain item in DB if it's name is duplicated
- NHibernate.ISession does not contain a definition for Linq
- LINQ to SQL-- Best learning resources?
- How to use a LINQ statement to get information from SharePoint
- OrderBy().ThenBy() wrong output
- How to get a custom object out of a generic List with LINQ?
- Sorting, filtering, and paging at DB side using LINQ to Entities without hand-written SQL
- How to count and sum total of DataTable with LINQ?
- FirstOrDefault() off of a LINQ versus FirstOrDefault() with a Lambda?
- LINQ - Assign a Value to Anonymous Type's Read Only Property
- Create a hierarchical JSON Http response structure using LinQ
- Array.BinarySearch where a certain condition is met
- Implementing method in a functional style
- LINQ SelectMany returns child instead of a parent in nested filtering
- Sort object that contains List and update object using LINQ
- How to Implement this Sql block contain (Having , Join and Group By) with Linq
- How to use anonymous type returned from a method
- Linq Aggregate complex types into a string
- Searching for dictionary keys contained in a string array
- Get results matching a list of Ids using Linq