Make NameValueCollection accessible to LINQ Query

.NetLinqNamevaluecollection

.Net Problem Overview


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<>.

.Net Solutions


Solution 1 - .Net

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]));
}

Edit: In response to @Ruben Bartelink's request, here is how to access the full set of values for each key using ToLookup:

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

    var pairs =
        from key in collection.Cast<String>()
        from value in collection.GetValues(key)
        select new { key, value };

    return pairs.ToLookup(pair => pair.key, pair => pair.value);
}

Alternatively, using C# 7.0 tuples:

public static IEnumerable<(String name, String value)> ToTuples(this NameValueCollection collection)
{
    if(collection == null)
    {
        throw new ArgumentNullException("collection");
    }

    return
        from key in collection.Cast<string>()
        from value in collection.GetValues(key)
        select (key, value);
}

Solution 2 - .Net

AsQueryable must take an IEnumerable<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"));
}

Solution 3 - .Net

I know I'm late to the party but just wanted to add my answer that doesn't involve the .Cast extension method but instead uses the AllKeys property:

var fields = RequestFields().AllKeys;

This would allow the following extension method:

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

    return collection.AllKeys.Select(key => new KeyValuePair<string, string>(key, collection[key]));
}

Hope this helps any future visitors

Solution 4 - .Net

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); 
    }
}

Solution 5 - .Net

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.

Solution 6 - .Net

For me, @Bryan Watts' (+1'd) answer's ToLookup variant represents by far the clearest approach for using it on a read-only basis.

For my use case, I'm manipulating a query string for use with Linq2Rest and also need to turn it all back into a NameValueCollection at the end, so I have a set of extension methods for NameValueCollection which offer more granular operations (to operate both per parameter name (AsEnumerable) and per argument (AsKeyValuePairs)) and also the inverse operation of converting it back ToNameValueCollection (from either representation)).

Example consumption:

public static NameValueCollection WithoutPagingOperators( this NameValueCollection that )
{
	return that.AsEnumerable()
		.Where( @param => @param.Key != OdataParameters.Skip 
		  && @param.Key != OdataParameters.Top )
		.ToNameValueCollection();
}

Code:

using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;

public static class NamedValueCollectionExtensions
{
	public static IEnumerable<KeyValuePair<string, string[]>> AsEnumerable( this NameValueCollection that )
	{
		return that
			.Cast<string>() // doesn't implement IEnumerable<T>, but does implement IEnumerable
			.Select( ( item, index ) => // enable indexing by integer rather than string
				new KeyValuePair<string, string[]>( item, that.GetValues( index ) ) ); // if you use the indexer or GetValue it flattens multiple values for a key, Joining them with a ',' which we don't want
	}

	public static IEnumerable<KeyValuePair<string, string>> AsKeyValuePairs( this IEnumerable<KeyValuePair<string, string[]>> that )
	{
		return that
			.SelectMany( item =>
				item.Value.Select( value =>
					new KeyValuePair<string, string>( item.Key, value ) ) );
	}

	public static NameValueCollection ToNameValueCollection( this IEnumerable<KeyValuePair<string, string[]>> that )
	{
		return that.AsKeyValuePairs().ToNameValueCollection();
	}

	public static NameValueCollection ToNameValueCollection( this IEnumerable<KeyValuePair<string, string>> that )
	{
		var result = new NameValueCollection();
		foreach ( KeyValuePair<string, string> item in that )
			result.Add( item.Key, item.Value );
		return result;
	}
}

Solution 7 - .Net

I don't really see why anyone would need to add an extension method.
Here's some different ways to do it in VB.NET. It includes 4 different intermediate forms of IEnumerable: Array, Tuple, Anonymous, and KeyValuePair. For the C# equivalent go to converter.telerik dot com and convert it.

Dim nvc As New NameValueCollection() From {{"E", "55"}, {"A", "11"}, {"D", "44"}, {"C", "33"}, {"G", "66"}, {"B", "22"}}

Dim dictStrings As Dictionary(Of String, String) = nvc.Cast(Of String).ToDictionary(Function(key) key, Function(key) nvc(key))
Dim Ints2Chars__ As Dictionary(Of Integer, Char) = nvc.Cast(Of Object).ToDictionary(Function(key) CInt(nvc(CStr(key))), Function(key) CChar(key))

Dim arrEnumerable__ = From x In nvc.Cast(Of String) Select {x, nvc(x)}
Dim tupleEnumerable = From x In nvc.Cast(Of String) Select Tuple.Create(x, nvc(x))
Dim anonEnumerable_ = From X In nvc.Cast(Of String) Select New With {X, .Y = nvc(X)}
Dim kvpEnumerable__ = From x In nvc.Cast(Of String) Select New KeyValuePair(Of String, String)(x, nvc(x))

Dim anonQuery = From anon In anonEnumerable_ Let n = CInt(anon.Y) Order By n Where n > 30 Select New With {.num = n, .val = anon.X}
Dim dictQuery = anonQuery.ToDictionary(Of Integer, String)(Function(o) o.num, Function(o) o.val)


Dim dictArray_ = arrEnumerable__.ToDictionary(Function(x) x(0), Function(x) x(1))
Dim dictTuples = tupleEnumerable.ToDictionary(Function(tuple) tuple.Item1, Function(tuple) tuple.Item2)
Dim dictAnon__ = anonEnumerable_.ToDictionary(Function(anon) anon.X, Function(anon) anon.Y)
Dim dictKVPrs_ = kvpEnumerable__.ToDictionary(Function(kvp) kvp.Key, Function(kvp) kvp.Value)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGravitonView Question on Stackoverflow
Solution 1 - .NetBryan WattsView Answer on Stackoverflow
Solution 2 - .NetAmy BView Answer on Stackoverflow
Solution 3 - .NetNinjaNyeView Answer on Stackoverflow
Solution 4 - .NetOrion AdrianView Answer on Stackoverflow
Solution 5 - .NetFrans BoumaView Answer on Stackoverflow
Solution 6 - .NetRuben BartelinkView Answer on Stackoverflow
Solution 7 - .NetDerek ZiembaView Answer on Stackoverflow