Q:

Selecting only unique values from collection

I have a collection of user objects, but need to return a list of distinct users based on User ID.
So I'm wondering is it possible to select only distinct values based on a given property of the user object?
Collection<User> users = serializer.Deserialize<Collection<User>>(userCollection);

User Object:
UserID
UserName

Thanks

A:

Use morelinq's DistinctBy() method.
var distinctUsers = users.DistinctBy(user => user.UserID);

Or craft the query on your own by grouping on what you want to get the distinction and take the first item in the group.
var distinctUsers = users.GroupBy(user => user.UserID)
                         .Select(g => g.First());

