If just want to user pure Linq, you can use groupby:
List<obj> distinct =
objs.GroupBy(car => car.typeID).Select(g => g.First()).ToList();
If you want a method to be used all across the app, similar to what MoreLinq does:
public static IEnumerable<TSource> DistinctBy<TSource, TKey>
(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
{
HashSet<TKey> seenKeys = new HashSet<TKey>();
foreach (TSource element in source)
{
if (!seenKeys.Contains(keySelector(element)))
{
seenKeys.Add(keySelector(element));
yield return element;
}
}
}
Using this method to find the distinct values using just the Id property, you could use:
var query = objs.DistinctBy(p => p.TypeId);
you can use multiple properties:
var query = objs.DistinctBy(p => new { p.TypeId, p.Name });