2

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();
3
  • might be way off but should that be g.Value? Commented Aug 16, 2010 at 4:45
  • No g.Value seemed to be available. Thanks for the comment though :) Commented Aug 16, 2010 at 4:56
  • In Linq, grouping actually returns groups (IGrouping), unlike SQL groupings which basically returns the "headers" of groups. Commented Aug 16, 2010 at 4:57

1 Answer 1

4

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;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.