How to remove duplicate records from a List<T> in C#

Jul 21 2021 5:15 PM

I want to remove the duplicates from the List, for that I am using following code:

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.


Answers (3)