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<>.
You need to ‘lift’ the non-generic
IEnumerableto anIEnumerable<string>. It has been suggested that you useOfTypebut that is a filtering method. What you’re doing is the equivalent of a cast, for which there is theCastoperator:As Frans pointed out, this only provides access to the keys. You would still need to index into the collection for the values. Here is an extension method to extract
KeyValuePairs from theNameValueCollection:Edit: In response to @Ruben Bartelink’s request, here is how to access the full set of values for each key using
ToLookup:Alternatively, using C# 7.0 tuples: