Skip to main content
Removed duplicates from the results
Source Link
Enrico Campidoglio
  • 60.8k
  • 13
  • 137
  • 159

Here's an alternative approach:

var items = new List<Tuple<string, int>>() { Tuple.Create("A", 3), Tuple.Create("A", 5), Tuple.Create("B", 1), Tuple.Create("C", 1), Tuple.Create("C", 3), Tuple.Create("C", 2) }; var results = items.GroupBy(i => i.Item1) .SelectMany(g => g .Where(i => i.Item2 == g.Max(m => m.Item2))) .Distinct(); 

Or if you prefer using the C# LINQ syntax:

results = (from item in items   group item by item.Item1 into groupedItems   let maxPriority = groupedItems.Max(item => item.Item2)   from element in groupedItems   where element.Item2 == maxPriority   select element;element).Distinct(); 

Here's an alternative approach:

var items = new List<Tuple<string, int>>() { Tuple.Create("A", 3), Tuple.Create("A", 5), Tuple.Create("B", 1), Tuple.Create("C", 1), Tuple.Create("C", 3), Tuple.Create("C", 2) }; var results = items.GroupBy(i => i.Item1) .SelectMany(g => g .Where(i => i.Item2 == g.Max(m => m.Item2))); 

Or if you prefer using the C# LINQ syntax:

results = from item in items group item by item.Item1 into groupedItems let maxPriority = groupedItems.Max(item => item.Item2) from element in groupedItems where element.Item2 == maxPriority select element; 

Here's an alternative approach:

var items = new List<Tuple<string, int>>() { Tuple.Create("A", 3), Tuple.Create("A", 5), Tuple.Create("B", 1), Tuple.Create("C", 1), Tuple.Create("C", 3), Tuple.Create("C", 2) }; var results = items.GroupBy(i => i.Item1) .SelectMany(g => g .Where(i => i.Item2 == g.Max(m => m.Item2))) .Distinct(); 

Or if you prefer using the C# LINQ syntax:

results = (from item in items   group item by item.Item1 into groupedItems   let maxPriority = groupedItems.Max(item => item.Item2)   from element in groupedItems   where element.Item2 == maxPriority   select element).Distinct(); 
Source Link
Enrico Campidoglio
  • 60.8k
  • 13
  • 137
  • 159

Here's an alternative approach:

var items = new List<Tuple<string, int>>() { Tuple.Create("A", 3), Tuple.Create("A", 5), Tuple.Create("B", 1), Tuple.Create("C", 1), Tuple.Create("C", 3), Tuple.Create("C", 2) }; var results = items.GroupBy(i => i.Item1) .SelectMany(g => g .Where(i => i.Item2 == g.Max(m => m.Item2))); 

Or if you prefer using the C# LINQ syntax:

results = from item in items group item by item.Item1 into groupedItems let maxPriority = groupedItems.Max(item => item.Item2) from element in groupedItems where element.Item2 == maxPriority select element;