Distinct by Property of Class with Linq

Distinct by property of class with LINQ

You can use grouping, and get the first car from each group:

List<Car> distinct =
.GroupBy(car => car.CarCode)
.Select(g => g.First())

LINQ's Distinct() on a particular property

EDIT: This is now part of MoreLINQ.

What you need is a "distinct-by" effectively. I don't believe it's part of LINQ as it stands, although it's fairly easy to write:

public static IEnumerable<TSource> DistinctBy<TSource, TKey>
(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
HashSet<TKey> seenKeys = new HashSet<TKey>();
foreach (TSource element in source)
if (seenKeys.Add(keySelector(element)))
yield return element;

So to find the distinct values using just the Id property, you could use:

var query = people.DistinctBy(p => p.Id);

And to use multiple properties, you can use anonymous types, which implement equality appropriately:

var query = people.DistinctBy(p => new { p.Id, p.Name });

Untested, but it should work (and it now at least compiles).

It assumes the default comparer for the keys though - if you want to pass in an equality comparer, just pass it on to the HashSet constructor.

Use LINQ to select distinct properties in Lists of Lists

You can do that like this, directly using the public DateCollection member:

var unique = master.DateCollection
.SelectMany(x => x.Data.Select(d => d.Value))

The key being SelectMany to "flatten" the selection.

LINQ query to return distinct field values from list of objects


Linq Get Distinct Values of Nested Elements

Without actually running it up and debugging... it's definitely possible, and I think it will look similar to:

MyNodes.SelectMany(x => x.Nodes.SelectMany(y => y.Properties.Select(z =>new KeyValuePair<string, string>(z.Name, z.Type))).Distinct().ToList();


Ran this up in LinqPad. My resulting query was:
nodes.SelectMany(n => n.Properties.Select(p => new KeyValuePair<string, string>(p.Name, p.Type))).Distinct().ToList();

Based on the class & initialization below:

public class Node{
public List<NodeProperty> Properties;

public class NodeProperty
public string Name;
public string Type;
var nodes = new List<Node>
new Node {
Properties = new List<NodeProperty>
new NodeProperty {Name = "node1", Type="string"},
new NodeProperty {Name = "node2", Type="int"},
new Node {
Properties = new List<NodeProperty>
new NodeProperty {Name = "node3", Type="string"},
new NodeProperty {Name = "node4", Type="int"},


Linq to get distinct property

Rewrite your GetHashCode() function:

public int GetHashCode(MY_DATA_TABLE obj)
return obj.CODE.GetHashCode();

Rule is, that both Equals and GetHashCode() should check the same properties, and you checking just Code in Equals() and whiole object in GetHashCode()

Distinct in LINQ-C#

The 3 records that you see at debug time is your existing list. I think all that you've missed is an assignment.

List<LinqTest> myList = new List<LinqTest>();
myList.Add(new LinqTest() { id = 1, value = "a" });
myList.Add(new LinqTest() { id = 1, value = "b" });
myList.Add(new LinqTest() { id = 2, value = "c" });
// not sure why new {m.id} was used in this context
List<int> distinctList = myList.Select(m => m.id).Distinct().ToList();

Related Topics

Leave a reply