using System.Collections.Generic;
using System.Data.DataSetExtensions;
public static void Main()
PrintTable(ApplyLogicTo(GetData()));
private static void PrintTable(DataTable dt)
foreach(var dr in dt.AsEnumerable())
Console.Write(dr.Field<string>("Name"));
Console.Write(dr.Field<int>("Value"));
private static DataTable ApplyLogicTo(DataTable dt)
.GroupBy(row => row.Field<string>("Name"))
.Select(grouping => grouping.MaxBy(row => row.Field<int>("Value")))
private static DataTable GetData()
var dt = new DataTable();
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Value", typeof(int));
public static class IEnumerableExtensions
public static TSource MaxBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> selector) where TKey : IComparable<TKey>
if (source == null) throw new ArgumentNullException(nameof(source));
if (selector == null) throw new ArgumentNullException(nameof(selector));
var bestElement = default(TSource);
var bestKey = default(TKey);
foreach(var element in source)
var key = selector(element);
if (key.CompareTo(bestKey) > 0)
bestKey = selector(element);
throw new ArgumentException("Sequence contains no elements", nameof(source));