John Smith
John Smith

Reputation: 315

LINQ distinct is showing duplicates

I am trying to use LINQ to find duplicates. From what I have read, I should use distinct. The query below finds the duplicates in the list, but it contains both the original value and its duplicate.

How can I get only the distinct items?

Class MacroConfig
{

    public Guid? GUID { get; set; }
    public string Name { get; set; }
    public string Value { get; set; }
}

//This is the linq query I am using
List<MacroConfig> dupeList = macroListWithDuplicates.GroupBy(x => x.GUID)
    .Where(y => y.Count() > 1)
    .SelectMany(y => y)
    .Distinct()
    .ToList();

foreach (var x in dupeList)
{
    Console.WriteLine(x.ToString());
}

Upvotes: 0

Views: 1580

Answers (4)

Dennis_E
Dennis_E

Reputation: 8894

Distinct() will return distinct MacroConfig objects, because MacroConfig's is what distinct() is receiving as input. If you only want to get 1 item for each GUID, you can just select the first item from each group:

List<MacroConfig> dupeList = macroListWithDuplicates
.GroupBy(x => x.GUID)
.Where(grp => grp.Count() > 1)
.Select(grp => grp.First())
.ToList();

Upvotes: 4

JLRishe
JLRishe

Reputation: 101690

How about this:

List<MacroConfig> dupeList = macroListWithDuplicates.GroupBy(x => x.GUID)
    .Select(y => y.First())
    .ToList();

Upvotes: 0

Eric J.
Eric J.

Reputation: 150108

How do you define a duplicate?

Distinct...

Returns distinct elements from a sequence by using the default equality comparer to compare values.

http://msdn.microsoft.com/en-us/library/vstudio/bb348436(v=vs.100).aspx

Have a look at the DistinctBy() operator in the MoreLinq NuGet package.

DistinctBy allows you to easily define exactly what constitutes a distinct entity.

Upvotes: 1

Sajeetharan
Sajeetharan

Reputation: 222582

var unique = from p in macroListWithDuplicates
                   group p by new {p.GUID } 
                   into mygroup
                   select mygroup.First();

Upvotes: 1

Related Questions