What you want is to be able to get the distinct items from a sequence, based on a selector, but keeping the items from the original sequence rather than the results of the selector. This is commonly named DistinctBy. MoreLinq has an implementation, which (with minor modifications) is:
public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector) { return source.DistinctBy(keySelector, null); } public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer) { return DistinctByImpl(source, keySelector, comparer); } private static IEnumerable<TSource> DistinctByImpl<TSource, TKey>(IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer) { HashSet<TKey> knownKeys = new HashSet<TKey>(comparer); foreach (TSource element in source) { if (knownKeys.Add(keySelector(element))) { yield return element; } } }
Using that, along with the CopyToDataTable method to convert the rows back into a table, you can now do:
var distinctTable = datatable.AsEnumerable() .DistinctBy(row => row.Field<string>("Employee")) .CopyToDataTable();
List<{anonymous type}>.Entityis a property, not a class.