Grouping in LINQ - simple question
I'm trying to get a list of the unique values from a field called 'domain' using the query below. Instead of just giving me the values themselves, my dropdown (drpDomain) contains values in this format: {Key = LNAU} etc. All I want to see is the value itself. Am I missing something obvious? Thanks!
var domainList = from v in db.Vendors
group 开发者_开发技巧v by new {v.Domain} into g
select g.Key;
drpDomain.DataSource = domainList;
drpDomain.DataBind();
Consider this, instead:
var domainList = from v in db.Vendors
select v.Domain;
drpDomain.DataSource = domainList.Distinct();
drpDomain.DataBind();
Note:
The real villain in your query is the anonymous object; however, the distinct query is more effective in communicating the intent, and may even be more efficient. That said, if you want only to fix the unexpected results of the grouping query, try this:
var domainList = from v in db.Vendors
group v by v.Domain into g
select g.Key;
精彩评论