Distinct in Linq based on only one field of the table

C#SqlLinq

C# Problem Overview


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.

C# Solutions


Solution 1 - C#

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.

Solution 2 - C#

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;
        }
    }
}

Solution 3 - C#

> but I need results where r.text is not duplicated

Sounds as if you want this:

table1.GroupBy(x => x.Text)
      .Where(g => g.Count() == 1)
      .Select(g => g.First());

This will select rows where the Text is unique.

Solution 4 - C#

Daniel Hilgarth's answer above leads to a System.NotSupported exception With Entity-Framework. With Entity-Framework, it has to be:

table1.GroupBy(x => x.Text).Select(x => x.FirstOrDefault());

Solution 5 - C#

There are lots of discussions around this topic.

You can find one of them here:

One of the most popular suggestions have been the Distinct method taking a lambda expression as a parameter as @Servy has pointed out.

The chief architect of C#, Anders Hejlsberg has suggested the solution here. Also explaining why the framework design team decided not to add an overload of Distinct method which takes a lambda.

Solution 6 - C#

From what I have found, your query is mostly correct. Just change "select r" to "select r.Text" is all and that should solve the problem. This is how MSDN documented how it should work.

Ex:

    var query = (from r in table1 orderby r.Text select r.Text).distinct();

Solution 7 - C#

data.Select(x=>x.Name).Distinct().Select(x => new SelectListItem { Text = x });

Solution 8 - C#

Distinct return you SINGLE column, if that's your case then!!

CountryData.Select(x=>x.COUNTRY_NAME).Distinct()

Else if you need multiple columns distinct by one column. you need to groupby first then select the first item from the group. (in this case the rest items in group with different values will not be returned)

CountryData.GroupBy(a=>a.COUNTRY_NAME).Select(a=>a.First());

Solution 9 - C#

try this code :

table1.GroupBy(x => x.Text).Select(x => x.FirstOrDefault());

Solution 10 - C#

You can try this:table1.GroupBy(t => t.Text).Select(shape => shape.r)).Distinct();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMegha JainView Question on Stackoverflow
Solution 1 - C#Daniel HilgarthView Answer on Stackoverflow
Solution 2 - C#ServyView Answer on Stackoverflow
Solution 3 - C#Tim SchmelterView Answer on Stackoverflow
Solution 4 - C#Biraj SahaView Answer on Stackoverflow
Solution 5 - C#TKharaishviliView Answer on Stackoverflow
Solution 6 - C#Josh ParksView Answer on Stackoverflow
Solution 7 - C#bgSView Answer on Stackoverflow
Solution 8 - C#sami ullahView Answer on Stackoverflow
Solution 9 - C#HamidRezaView Answer on Stackoverflow
Solution 10 - C#LucaGuerraView Answer on Stackoverflow