How to remove duplicate records from a List<T> in C# and return the unique records

kaushik.debnath

New member
Joined
Jul 21, 2021
Messages
1
Programming Experience
10+
I want to remove the duplicates from the List, for that I am using following code:
C#:
public List<ContainerLevelDetailsResponse> ContainerLevelDetails { get; set; }

public class ContainerLevelDetailsResponse
    {  
        public long ContainerId { get; set; }
        public string Container { get; set; }
        public string Pallet { get; set; }
        public string SKU { get; set; }
        public long CaseCount { get; set; }
    }

List<ContainerLevelDetailsResponse> containerLevelDetails =
new List<ContainerLevelDetailsResponse>();

booking.ContainerLevelDetails = containerLevelDetails.Distinct().ToList<ContainerLevelDetailsResponse>();
But its not working and did not giving unique records. Its not able to remove the duplicates.

Please suggest in this.
 
Last edited by a moderator:

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,364
Location
Chesapeake, VA
Programming Experience
10+
Your call to Distinct() is not passing in a lambda which will let it discriminate whether two responses are the same or not.
 

JohnH

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
1,194
Location
Norway
Programming Experience
10+
Top Bottom