Thursday, February 18, 2010

Get URL Parameters using LINQ

Dictionary result = new Dictionary();

String urlString = "http://www.jwize.com?param1=valu1¶m2=value2";

var query = from match in urlString.Split('?').Where(m => m.Contains('='))
.SelectMany(pr => pr.Split('&'))
where match.Contains('=')
select new KeyValuePair(
match.Split('=')[0],
match.Split('=')[1]);
query.ToList().ForEach(kvp => result.Add(kvp.Key, kvp.Value));

No comments: