How to get the distinct data from a list?

asp.net, c#, distinct, linq, list

Solution

`Distinct()` will give you distinct values - but unless you've overridden `Equals` / `GetHashCode()` you'll just get distinct references. For example, if you want two `Person` objects to be equal if their names are equal, you need to override `Equals`/`GetHashCode` to indicate that. (Ideally, implement `IEquatable<Person>` as well as just overriding `Equals(object)`.)

You'll then need to call `ToList()` to get the results back as a `List<Person>`:

var distinct = plst.Distinct().ToList();

If you want to get distinct people by some specific property but that's not a suitable candidate for "natural" equality, you'll either need to use `GroupBy` like this:

var people = plst.GroupBy(p => p.Name)
                 .Select(g => g.First())
                 .ToList();

or use the `DistinctBy` method from MoreLINQ:

var people = plst.DistinctBy(p => p.Name).ToList();

Problem

I want to get distinct list from list of persons . ``` List<Person> plst = cl.PersonList; ``` How to do this through `LINQ`. I want to store the result in `List<Person>`

Original source