No, you cant. You can, in the way that @Hogan says in his answer (https://stackoverflow.com/a/22815177/806975)
Also, you can use Select
before Distinct
, if you want to make distinct for a specific field.
list.Select(x => x.Id).Distinct();
However, this will return only the selected value (Id
).
Based on the question referred by @Patrick in his comment above (Distinct() with lambda?), and trying to add something to it, you can do an extension method to make what you want to do:
namespace MyProject.MyLinqExtensions
{
public static class MyLinqExtensions
{
public static System.Collections.Generic.IEnumerable<TSource> DistinctBy<TSource, TKey>(this System.Collections.Generic.IEnumerable<TSource> list, System.Func<TSource, TKey> expr)
{
return list.GroupBy(expr).Select(x => x.First());
}
}
}
Then, you can use it this way:
list.DistinctBy(x => x.Id)
Just remember to import the namespace in the class where you want to use it: using MyProject.MyLinqExtensions;