USING MORELINQ TO SELECT DISTINCT BY PROPERTY NAME

LINQ provides two overridden functions to select distinct elements only, either by default equality comparer or user defined one. However, there are cases where elements are to be selected only on base of a single or some set or properties.
This can be achieved by user defined set of codes that takes property as argument to filter by.
Below is the code:

public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
        {
            var seenKeys = new HashSet<TKey>();
            return source.Where(element => seenKeys.Add(keySelector(element)));
        }


The code below is used as method extension for IEnumerable DataSource. And using the defined key selector, the IEnumerable List if filtered by distinct based on the key selector.
e.g
var listSamples=new List<MyObject>();
var newdistinctList=listSamples.DistinctBy(o=>o.MyProperty);

However for selecting mutliple columns unique, following code can be used:
var newDistinctList2=listSamples.DistinctBy(o=> new {o.MyProperty1, o.MyProperty2});