views:

2492

answers:

4

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 soure is not IEnumerable<>.

+1  A: 

The problem is that the collection does implement IEnumerable, but enumerating the collection returns the keys, not the pairs.

if I were you, I'd use a Dictionary which is enumerable and which you can use with Linq.

Frans Bouma
Thanks, but I don't quite understand why despite that NameValueCollection implements IEnumerable, it still says that source is not IEnumerable.
Ngu Soon Hui
It implements IEnumerable (on the base class) but it doesn't implement IEnumerable<T> (the generic variant) so you can't use the extension methods which require the generic variant.
Frans Bouma
+2  A: 

AsQueryable must take an IEnumerable(Of T), a generic. NameValueCollection implements IEnumerable, which is different.

Instead of this:

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

Try OfType (it accepts the non-generic interface)

{
    NameValueCollection nvc = RequestFields();
    IEnumerable<string> canBeQueried = nvc.OfType<string>();
    IEnumerable<string> query =
       canBeQueried.Where(s => s.StartsWith("abc"));
}
David B
Good solution! I needed an easy way to search through the Request.Params NameValueCollection for a specific pattern, and this little tidbit of code helped me get there.
Jagd
+17  A: 

You need to "lift" the non-generic IEnumerable to an IEnumerable<string>. It has been suggested that you use OfType but that is a filtering method. What you're doing is the equivalent of a cast, for which there is the Cast operator:

var fields = RequestFields().Cast<string>();

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 the NameValueCollection:

public static IEnumerable<KeyValuePair<string, string>> ToPairs(this NameValueCollection collection)
{
    if(collection == null)
    {
        throw new ArgumentNullException("collection");
    }

    return collection.Cast<string>().Select(key => new KeyValuePair<string, string>(key, collection[key]));
}
Bryan Watts
One for the utils lib :)
borisCallens
A: 

A dictionary is probably actually closer to what you want to use since it will actually fill more of the roles that NameValueCollection fills. This is a variation of Bryan Watts' solution:

public static class CollectionExtensions
{
    public static IDictionary<string, string> ToDictionary(this NameValueCollection source)
    {
        return source.Cast<string>().Select(s => new { Key = s, Value = source[s] }).ToDictionary(p => p.Key, p => p.Value); 
    }
}
Orion Adrian