Maybe i missing using ? (i have using System.Li).
with Distinct
no problem.
This is my command that i want to add DistinctBy
List<Capture> list = db.MyObject.Where(x => x.prop == "Name").ToList();
You can add a extension method
public static IEnumerable<T> DistinctBy<T, TKey>(this IEnumerable<T> items, Func<T, TKey> property)
{
return items.GroupBy(property).Select(x => x.First());
}
and You can use it like
List<Capture> list = db.MyObject.Where(x => x.prop == "Name")
.DistinctBy(y=> y.prop )
.ToList();
OR, You can use DistincyBy
provided through MoreLinq.
Another example:
public static class ExtensionMethods
{
public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
{
var seenKeys = new HashSet<TKey>();
foreach (TSource element in source)
{
if (seenKeys.Add(keySelector(element)))
{
yield return element;
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With