Alex Rivera | Logout

Make NameValueCollection accessible to LINQ Query

Asked 2008-12-24T08:28:47.250
67

How to make NameValueCollection accessible to LINQ query operator such as where, join, groupby?

I tried the below:

private NameValueCollection RequestFields()
{
    NameValueCollection nvc = new NameValueCollection()
                                  {
                                      {"emailOption: blah Blah", "true"},
                                      {"emailOption: blah Blah2", "false"},
                                      {"nothing", "false"},
                                      {"nothinger", "true"}
                                  };
    return nvc;

}

public void GetSelectedEmail()
{
    NameValueCollection nvc = RequestFields();
    IQueryable queryable = nvc.AsQueryable();
}

But I got an ArgumentException telling me that the source is not IEnumerable<>.

Edit
Report

1 Answer

4

The problem is that the collection implements IEnumerable (as opposed to IEnumerable<T>) and enumerating the collection returns the keys, not the pairs.

If I were you, I'd use a Dictionary<string, string> which is enumerable and can be used with LINQ.

answered 2008-12-24T08:36:52.320

Your Answer