Remove objects with duplicate properties from List

c#, linq, list

Solution

To use multiple properties you can use an anonymous type:

var query = fooList.GroupBy(x => new { x.Dept, x.Course })
                   .Select(x => x.First());

Of course, this depends on what types `Dept` and `Course` are to determine equality. Alternately, your classes can implement `IEqualityComparer<T>` and then you could use the `Enumerable.Distinct` method that accepts a comparer.

Problem

I have a List of objects in C#. All of the objects contain the properties dept and course. There are several objects that have the same dept and course. How can I trim the List(or make a new List) where there is only one object per unique (dept & course) properties. [Any additional duplicates are dropped out of the List] I know how to do this with a single property: ``` fooList.GroupBy(x => x.dept).Select(x => x.First()); ``` However, I am wondering how to do this for multiple properties (2 or more)?

Original source