I am trying to use .distinct in Linq to get result based on one field of the table (so do not require a whole duplicated records from table).
I know writing basic query using distinct as followed:
var query = (from r in table1
orderby r.Text
select r).distinct();
but I need results where r.text
is not duplicated.
Try this:
table1.GroupBy(x => x.Text).Select(x => x.FirstOrDefault());
This will group the table by Text
and use the first row from each groups resulting in rows where Text
is distinct.
MoreLinq has a DistinctBy method that you can use:
It will allow you to do:
var results = table1.DistictBy(row => row.Text);
The implementation of the method (short of argument validation) is as follows:
private static IEnumerable<TSource> DistinctByImpl<TSource, TKey>(IEnumerable<TSource> source,
Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
{
HashSet<TKey> knownKeys = new HashSet<TKey>(comparer);
foreach (TSource element in source)
{
if (knownKeys.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