C#: Convert Dictionary<> to NameValueCollection

后端 未结 3 1368
执念已碎
执念已碎 2021-01-31 03:42

How can I convert a Dictionary to a NameValueCollection?

The existing functionality of our project returns an old-fashion

3条回答
  •  借酒劲吻你
    2021-01-31 04:03

    And if you like LINQ:

    Dictionary to NameValueCollection

    return dictionary.Aggregate(new NameValueCollection(),
        (seed, current) => { 
            seed.Add(current.Key, current.Value);
            return seed;
        });
    

    NameValueCollection to Dictionary

    (source)

    return source.Cast()  
        .ToDictionary(s => s, s => source[s]);
    

提交回复
热议问题