score:13
Accepted answer
If you don't override Equals
and GetHashCode
in your class or provide a custom equality comparer Distinct
method uses default equality comparer for the type.And it compares reference types by references. Not by the property values. If you don't want this behaviour either override the relevant methods in your class or if you can't change the class implement an IEqualityComparer
for the type and pass it to Distinct
:
var ResourceTypeNameList = Resources
.Select(r => new bl_SelectBox{ text=r.ResourceTypeName, value=r.resourceTypeID })
.Distinct(new MyEqualityComparer());
Another quick solution would be using GroupBy
:
var ResourceTypeNameList = Resources
.Select(r => new bl_SelectBox{ text=r.ResourceTypeName, value=r.resourceTypeID })
.GroupBy(x => x.SomeProperty)
.Select(x => x.First());
Source: stackoverflow.com
Related Articles
- LINQ Select Distinct Count in Lambda form
- Lambda Distinct Select
- How to write a Lambda Expression to select distinct words from list of sentences
- How to select entire row by distinct column MVC code first
- Select distinct using linq
- Entity Framework select distinct name
- LINQ Select Distinct with Anonymous Types
- Linq code to select one item
- How to use Lambda in LINQ select statement
- Select All distinct values in a column using LINQ
- How to get distinct instance from a list by Lambda or LINQ
- How do I write this lambda select method in VB.net?
- lambda expression join multiple tables with select and where clause
- Select distinct values from a list using LINQ in C#
- Select distinct rows from datatable in Linq
- Use LINQ to select distinct properties in Lists of Lists
- This code returns distinct values. However, what I want is to return a strongly typed collection as opposed to an anonymous type
- LINQ How to select more than 1 property in a lambda expression?
- Select Distinct List of Words from Array with LINQ
- Select every second element from array using lambda
- Select from list Lambda or linq
- linq distinct and select new query
- Select a model property using a lambda and not a string property name
- How do I select the distinct row count of a column in a data table?
- Lambda Expression of Select * from TableName
- Linq - select distinct values
- Compound Select using lambda expression
- Select distinct from List<t> ignoring one element
- Multiple Select and Join with LINQ and Lambda
- LINQ Select Distinct while ignoring the XML field
- How do I get a spatial value from EF Core's FromSqlRaw
- generalise where clause in linq query
- Why foreach loop works but Linq variant doesn't?
- How to get specific columns from different tables ASP.NET Core and Entity Framework Core
- Count occurrences of values across multiple columns
- Unable to cast nvarchar to datetime when calling SQL Server stored procedure from LINQ to SQL
- Read Foursquare Json Response with JSON.NET
- How do I cast this using LINQ?
- How to efficiently rewrite LINQ expression 'DbSet.Where(...Any(...))' that could not be translated in EFCore 3.1?
- Adding a random Guid column to a Linq to Entities query to grab random records
- Search dictionary values and return List of keys meeting conditions
- How can i write SQL update query with where clause in Entity Framework in C#
- LINQ - Return Value From Field With A Max Value if No Rows Found
- Optimization in the SingleOrDefault function of Linq
- Entity Framework with LINQ using CONTAINS in WHERE clause very slow with large integer list
- LINQ create new list from elements of outer and inner list
- convert Conditional Join in SQL to Linq
- Sorting xml using linq, but getting a null error?
- get distinct count and name using linq from dataset table
- How to return just first parent element, but many children and grandchildren in Linq with Entity Framework - SYBASE ASE Connector bug