7

I have the below list and query expression to get the distinct list.

 List<LinqTest> myList = new List<LinqTest>();
 myList.Add(new LinqTest() { id = 1, value = "a" });
 myList.Add(new LinqTest() { id = 1, value = "b" });
 myList.Add(new LinqTest() { id = 2, value = "c" });
 myList.Select(m => new { m.id}).Distinct().ToList();

I have referred the below SO link linkfor the code. Even after using distinct() i'm getting 3 records with the duplicate values. what could be the reason?

3

6 Answers 6

11

The 3 records that you see at debug time is your existing list. I think all that you've missed is an assignment.

List<LinqTest> myList = new List<LinqTest>();
myList.Add(new LinqTest() { id = 1, value = "a" });
myList.Add(new LinqTest() { id = 1, value = "b" });
myList.Add(new LinqTest() { id = 2, value = "c" });
// not sure why new {m.id} was used in this context
List<int> distinctList = myList.Select(m => m.id).Distinct().ToList();
0
5

In your case the Distinct() method uses the default equality comparer of anonimous type object. Therefore the result is not expected. If you want to get unique id values use

List<int> ListOfIDs = myList.Select(m => m.id).Distinct().ToList();

If you want to get distinct objects of a custom type, then you need to implement IEquatable generic interface and provide your own GetHashCode and Equals methods for the type. See MSDN for details.

3

You are not storing the distinct list to anywhere. change you last line to assign the distinct values to a new variable.

 var distinctList = myList.Select(m => new { m.id}).Distinct().ToList();
2

Use GroupBy,

var result  = myList 
  .GroupBy(p => p.id)
  .Select(g => g.First())
  .ToList();
1
  • Thank you. Any guesses on why distinct() is not working in this case? May 30, 2017 at 4:42
2

Change your select as .Select(m => m.id), you are creating new object with id as property in it, then after get the distinct, when comparing objects will not give the exact distinct objects you need. you need to be specific, if you need to get distinct by property of object then use GROUP BY or MoreLinq, DistinctBy method. check answers of below question

Distinct in Linq based on only one field of the table

1

Here's an example that does this (note Equals and GetHashCode methods):

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<LinqTest> myList = new List<LinqTest>();
            myList.Add(new LinqTest() { Id = 1, Value = "a" });
            myList.Add(new LinqTest() { Id = 1, Value = "b" });
            myList.Add(new LinqTest() { Id = 2, Value = "c" });

            var distinct = myList.Distinct().ToList();
        }

        class LinqTest
        {
            public override bool Equals(object obj)
            {
                var that = obj as LinqTest;
                return that == null ? false : Id == that.Id;
            }

            public override int GetHashCode()
            {
                return Id;
            }

            public int Id { get; set; }
            public string Value { get; set; }
        }
    }
}
1
  • However, this wouold always consider two LinqTest objects with the same id as equal, which is probably not what they want at any other time in their application. This seems to be just a one-off to get a list of IDs, unrelated to what is considered "equal" May 30, 2017 at 13:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.