Link to home
Start Free TrialLog in
Avatar of karthik80c
karthik80cFlag for United States of America

asked on

How to remove Duplicates from Generic List in Csharp

Hi Experts ,

I want to remove duplicated data from a generic list using c sharp Function. Is there any built in function or custom function to do this

Thanks in Advance Experts
Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

You could use Linq Distinct as shown here
Avatar of karthik80c

ASKER

i used this method and it shows error as the context in not available
Hi karthik80c;

Please show the code you are using as well as the data type of the objects in the List. If the objects in the list are of a user defined type please post here and also on which field/s you want the no duplicates / Distinct field to be of.
ASKER CERTIFIED SOLUTION
Avatar of ROMA CHAUHAN
ROMA CHAUHAN
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@ROMA CHAUHAN

You can use the Distinct method to get the unique values.
Yes and no. You can use Distinct as you mention, but you will only get truly distinct values from either collections of simple types or types which implement IComparable. For complex types, you either have to implement IComparable on your type, or you have to use the overload of Distinct which takes in a IEqualityComparer. A final option would be to use Jon Skeet's MoreLinq library in which he and his team implemented a DistinctBy method, where you can pass in a lambda expression rather than having to create an IEqualityComparer.