How can I convert a NameValueCollection to a KeyValuePair

namevaluecollection to querystring
namevaluecollection to dictionary
toquerystring c#

I want to convert a NameValueCollection to a KeyValuePair. Is there a way to do this easily for just a single value in a NameValueCollection?

I have this right now but it seems kind of verbose:

private KeyValuePair<string, string> GetEtagHeader(NameValueCollection collection)
{
    var etagValue = collection.Get(HttpRequestHeader.IfMatch.ToString());

    return new KeyValuePair<string, string>(HttpRequestHeader.IfMatch.ToString(), etagValue);
}

I'm not sure how much shorter you can get it.

One possibility is to put the Get in where you create the KeyValuePair

private static KeyValuePair<string, string> GetEtagHeader(NameValueCollection collection)
{
    string key = HttpRequestHeader.IfMatch.ToString();
    return new KeyValuePair(key, collection.Get(key));
}

That should serve your case. I'd go a step further and split it into 2 methods - one for your specific case and one generic helper.

private static KeyValuePair<string, string> GetEtagHeader(NameValueCollection collection)
{
    return ToKeyValuePair(HttpRequestHeader.IfMatch.ToString(), collection);
}

private static KeyValuePair<string, string> ToKeyValuePair(string key, NameValueCollection collection)
{
    return new KeyValuePair(key, collection.Get(key));
}

foreach KeyValuePair in NameValueCollection?, First of all, NameValueCollection doesn't use KeyValuePair<String,String> . NameValueCollection nv = HttpUtility. Cast<string>()) writer. But the question is why you cannot convert NameValueCollection items to KeyValuePair<string, string>. Maybe you read the question not very carefully. The best answer was given by jgauffin. An interesting solution was proposed by Adrian Carneiro. – Warlock Jul 26 '14 at 16:15

It would be less verbose if you put HttpRequestHeader.IfMatch.ToString() into a temp variable and instead inline the temp etagValue:

private KeyValuePair<string, string> GetEtagHeader(NameValueCollection collection)
{
    string key = HttpRequestHeader.IfMatch.ToString();
    return new KeyValuePair<string, string>(key, collection.Get(key));
}

How to convert NameValueCollection to a (Query) String, Most ASP.NET developers know that you can get a key/value pair string from the Request.QueryString object (via the .ToString() method). How do I convert the string keys and values in the NameValueCollection to the generic type in this method so I can load those to my generic dictionary object – Kobojunkie May 14 '13 at 18:08 1 @Kobojunkie There is no "general purpose" way to do this - you need some type of method to do the conversion.

If it were me, I'd define an extension method like this one:

public static class ExtensionMethods
{
    static public KeyValuePair<string,string> GetPair(this NameValueCollection source, string key)
    {
        return new KeyValuePair<string, string>
        (
            key,
            source.Get(key)
        );
    }
}

Then you can just write your original code like this:

private KeyValuePair<string, string> GetEtagHeader(NameValueCollection collection)
{
    return collection.GetPair(HttpRequestHeader.IfMatch.ToString());
}

NameValueCollection Class (System.Collections.Specialized , Convert MVC5's QueryString to a List<KeyValuePair<string, StringValues>> needed by ShopifySharp's auth service. example.cs NameValueCollection qs). Most ASP.NET developers know that you can get a key/value pair string from the Request.QueryString object (via the .ToString() method). However that functionality isn't the same for a generic NameValueCollection object (of which Request.QueryString is derived from). So how do you take a NameValueCollection object and get a nicely formatted key/value pair string? (i.e. "key1=value1&amp;key2

Convert MVC5's QueryString to a List<KeyValuePair<string , Specialized.NameValueCollection - a type that does not exist in . Any method which converts the collection to a dictionary will fail in this case. I assumed your NameValueCollection was in the form of string -> object. Adjust the " item.Value " part if it differs. I also didn't URL Encode the key of a pair, since i don't think they accept url-encoded keys.

Cannot pass MVC5 Request.QueryString or Request.Headers to , This C# tutorial shows the NameValueCollection type. It provides You can also remove a key-value pair using the Remove method on NameValueCollection. C# (CSharp) System.Collections.Specialized NameValueCollection.Cast - 20 examples found. These are the top rated real world C# (CSharp) examples of System.Collections.Specialized.NameValueCollection.Cast extracted from open source projects. You can rate examples to help us improve the quality of examples.

C# NameValueCollection Usage, Specialized NameValueCollection. Cast extracted from open source projects. Select( key => new KeyValuePair<string, string>(key, requestParams[key]) )  How can I convert a Dictionary<string, string> to a NameValueCollection? The existing functionality of our project returns an old-fashioned NameValueCollection which I modify with LINQ. The result should be passed on as a NameValueCollection. I want to solve this in a generic way. Any hints?

Comments
  • Keep in mind that name value collection - like http headers allows single key with multiple values.
  • These methods should be static since they don't reference any members of the enclosing class. Better yet, they could be extension methods with a this NameValueCollection collection parameter.
  • The method should be static since it doesn't reference any members of the enclosing class. Better yet, it could be an extension method with a this NameValueCollection collection parameter.
  • Yes, unless HttpRequestHeader is an instance property.