Skip to content

Instantly share code, notes, and snippets.

@NickDarvey
Last active June 12, 2017 15:17
Show Gist options
  • Save NickDarvey/dde2213d9c8d659f693a3a109b101f48 to your computer and use it in GitHub Desktop.
Save NickDarvey/dde2213d9c8d659f693a3a109b101f48 to your computer and use it in GitHub Desktop.
NameValueCollection to IEnumerable, ILookup
// https://stackoverflow.com/questions/391023/make-namevaluecollection-accessible-to-linq-query
using System;
using System.Collections.Specialized;
using System.Linq;
namespace System.Collections.Specialized
{
public static class Extensions
{
public static ILookup<string, string> ToLookup(this NameValueCollection @this)
{
if (@this == null) throw new ArgumentNullException(nameof(@this));
return @this
.Cast<string>()
.SelectMany(key => @this.GetValues(key), (key, value) => new { key, value })
.ToLookup(p => p.key, p => p.value);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment