.NET Fiddle

CSharp · Console

/Program.cs

using System;
using System.Linq;
using System.Collections.Generic;

// see: https://stackoverflow.com/a/24952248/1016343
public static class Extensions
{
    public static IEnumerable<T> MyDistinct<T, V>(this IEnumerable<T> query,
                                                    Func<T, V> f, 
                                                    Func<IGrouping<V,T>,T> h=null)
    {
        if (h==null) h=(x => x.First());
        return query.GroupBy(f).Select(h);
    }
	
	public static IEnumerable<T> MyDistinct2<T, V>(this IEnumerable<T> query,
                                                Func<T, V> f,
                                                Func<T,bool> h=null)
	{
		if (h == null) h = (y => true);
		return query.GroupBy(f).Select(x=>x.FirstOrDefault(h));
	}
}

public class MyObject
{
    public string Name;
    public string Code;
}
					
public class Program
{
	public static void Main()
	{
		MyObject[] _myObject = {
			new MyObject() { Name = "Test1", Code = "T"},
			new MyObject() { Name = "Test2", Code = "Q"},
			new MyObject() { Name = "Test2", Code = "T"},
			new MyObject() { Name = "Test5", Code = "Q"}
		};
		
		Console.WriteLine("First example");
		var myQuery=(from x in _myObject select x).MyDistinct(d => d.Code);
		myQuery.Dump();
		
		Console.WriteLine(); Console.WriteLine(); Console.WriteLine("Second example");
		var myQuery2 = (from x in _myObject select x).MyDistinct(d => d.Name,
        					x=>x.FirstOrDefault(y=>y.Name.Contains("1") || y.Name.Contains("2"))
        				);
		myQuery2.Dump();
		
		Console.WriteLine(); Console.WriteLine(); Console.WriteLine("Third example");
		var myQuery3 = (from x in _myObject select x).MyDistinct2(d => d.Name,
                    		y => y.Name.Contains("1") || y.Name.Contains("2")
                    	);
		myQuery3.Dump();
		
	}
}
;