Last active
June 12, 2017 15:17
-
-
Save NickDarvey/dde2213d9c8d659f693a3a109b101f48 to your computer and use it in GitHub Desktop.
NameValueCollection to IEnumerable, ILookup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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