Easy:
var people = new List<Person>(); // code to populate people var uniqueNameAges = (from p in people select new { p.Name, p.Age }).Distinct();
And then into the dictionary:
var dictionary = uniqueNameAges .ToDictionary(x => x.Name, x => x.Age);
Or for a search (very similar to Dictionary<string, IEnumerable<int>> in this case):
var lookup = uniqueNameAges .ToLookup(x => x.Name, x => x.Age);
If you have people with the name "John" with different ages, you can access them like this:
IEnumerable<int> ages = lookup["John"];
source share