Select DISTINCT on a column when selecting multiple columns in Linq
I'm selecting multiple columns from a table (id, name). I would like to get back records with distinct name. I'm wondering how to do this in LINQ.
The following code returns a Dictionary<string,string>
return db.CourseTypes
.Select(ct => new { ct.Id, ct.Name})
.AsEnumerable()
.ToDictionary(kvp => kvp.Id.ToString(), kvp => kvp.Name);
}
How can I make sure to get back records with distinct values in the 'Name' 开发者_开发知识库column?
A slightly simpler approach than StriplingWarrior's (I think) and with slightly fewer issues:
return db.CourseTypes
.GroupBy(ct => ct.Name, ct => ct.Id)
.ToDictionary(group => group.First(), group => group.Key);
Note that this assumes your IDs are also unique - if two different names have the same ID, ToDictionary
will fail.
Also note that if you've got multiple IDs with the same name, it's assuming you don't really care which one is in the map. If you want to make sure it's (say) the earliest name you could do:
return db.CourseTypes
.GroupBy(ct => ct.Name, ct => ct.Id)
.ToDictionary(group => group.OrderBy(x => x).First(),
group => group.Key);
Now it's possible that that will do the Queryable to Enumerable transition at the wrong point, so you might want:
return db.CourseTypes
.GroupBy(ct => ct.Name, ct => ct.Id)
.Select(g => new { Name = g.OrderBy(x => x).First(), Id = g.Key })
.ToDictionary(g => g.Name, g => g.Id);
That's more likely to do all the ordering etc within the database in one go... I think... it's worth checking the logs though.
精彩评论