SoFunction
Updated on 2025-03-08

Perfectly solve the problem of C# distinct not working

When you want to use the following code when you want to use a deduplication method based on a certain field in a combination

IQueryable inherits from IEnumerable

Let me give you an example first:

#region linq to object 
List<People> peopleList = new List<People>();
(new People { UserName = "zzl", Email = "1" });
(new People { UserName = "zzl", Email = "1" });
(new People { UserName = "lr", Email = "2" });
(new People { UserName = "lr", Email = "2" });

("Use extension method to filter a field and then output the current entity");
(i => new {  }).ToList().ForEach(i => ( + ));
("Default method, there are multiple fields in the collection, distinct takes effect when all fields are repeated, this is the same as SQLSERVER");
(i => new { UserName = , Email =  }).OrderByDescending(k => ).Distinct().ToList().ForEach(i => ( + ));
("There is a field in the collection, filter this field repeatedly and output this field");
(i => new {  }).Distinct().ToList().ForEach(i => ());

#endregion

This extension method is posted:

public static class EnumerableExtensions
{
public static IEnumerable<TSource> DistinctBy<TSource, Tkey>(this IEnumerable<TSource> source, Func<TSource, Tkey> keySelector)
{
HashSet<Tkey> hashSet = new HashSet<Tkey>();
foreach (TSource item in source)
{
if ((keySelector(item)))
{
yield return item;
}
}
 }
}

Supplementary knowledge:c# - .Distinct() call does not filter

I'm trying to pull an Entity Framework DbContext query into an IEnumerable<SelectListItem> using AsEnumerable. This will be used as a model property to fill the dropdown list in the view.

However, despite the call to Distinct(), each query returns a duplicate entry.

public IEnumerable<SelectListItem> StateCodeList { get; set; }
public IEnumerable<SelectListItem> DivCodeList { get; set; }  

DivCodeList =
  ().OrderBy(x => ).Distinct().Select(x => new SelectListItem
          {
            Text = ,
            Value = 
          }).ToList();

StateCodeList =
  ().OrderBy(x => ).Distinct().Select(x => new SelectListItem
          {
            Text = ,
            Value = 
          }).ToList();

In order for Distinct to take effect, if the type is a custom type, the sequence must contain objects of the type that implements the IEquatable interface.

As stated here:

Distinct returns distinct elements from a sequence by using the

default equality comparer to compare values.

A workaround, to avoid the above, because I can conclude that you don't need the entire object instead of one of its properties, is to project each element of the sequence into Division, then create OrderBy and call Distinct:

var divisions = ()
   .Select(ml=>)
   .OrderBy(division=>division) 
   .Distinct()
   .Select(division => new SelectListItem
   {
     Text = division,
     Value = division
   }).ToList();

For further documentation on this issue, please checkhere.

The above article perfectly solves the problem of C# distinct's not easy to use is all the content I share with you. I hope you can give you a reference and I hope you support me more.