如何对 NameValue 集合进行排序并将其存储在字符串中

How to Sort NameValue Collection and Store it in a string

这里我将两组查询字符串参数存储到两个不同的名称值集合中。查询字符串参数顺序可能会有所不同,所以我只想对顺序进行排序,然后我需要将名称值集合存储到一个字符串中。

更新代码:

string url1 = @"http://www.somewebsitesampletest.com/dcs7o?data=142248494&dcp=smre&nparam=4567P&email=xxx.com";

string url2 = @"http://www.somewebsitesampletest.com/dcs7o?dcp=smre&data=142248494&email=xxx.com&nparam=4567P";

var NameValueCollection1 = HttpUtility.ParseQueryString(url1);
var NameValueCollection2 = HttpUtility.ParseQueryString(url2);

预期结果: 排序并转换为字符串后,结果应如下所示

string query1 = "data=142248494&dcp=smre&email=xxx.com&nparam=4567P";
string query2 = "data=142248494&dcp=smre&email=xxx.com&nparam=4567P";

这是一个使用 Linq 的解决方案。

基本上它使用 Cast<T>NameValueCollection 更改为 IEnumerable 键,然后其余部分不言自明。

public string GetSortedQueryString(string url)
{
    var queryString = HttpUtility.ParseQueryString(url);

    // Ignore null keys (caused by your ?& at the start of the query string
    var orderedKeys = queryString.Cast<string>().Where(k => k != null).OrderBy(k => k);

    return string.Join("&", orderedKeys.Select(k => string.Format("{0}={1}", k, queryString[k])));
}

您的网址的结果将是:

data=142248494&dcp=smre&email=xxx.com&nparam=4567P
data=142248494&dcp=smre&email=xxx.com&nparam=4567P

电子邮件出现在 nparam 之前,与您预期的解决方案不同(我假设这是一个错误)。

LINQDictionaryKeyValuePairlist 一起使用:

string url1 = @"http://www.somewebsitesampletest.com/dcs7o?&data=142248494&dcp=smre&nparam=4567P&email=xxx.com";
string query1 ="";
Dictionary<String, String> paramDict = new Dictionary<string, string>();

var query = from match in urlString.Split('?').Where(m => m.Contains('='))
                .SelectMany(pr => pr.Split('&'))
            where match.Contains('=')
            select new KeyValuePair<string, String>(
                match.Split('=')[0],
                match.Split('=')[1]);

query.ToList().ForEach(kvp => paramDict.Add(kvp.Key, kvp.Value));

var List<KeyValuePair<string, string>> paramList = paramDict.ToList();

paramList.Sort();

foreach (KeyValuePair<string, int> pair in list)
{
    query1+=pair.Key+"="+pair.Value+"&";
}
query1=query1.TrimEnd('&');

我做这个 fiddle 因为我需要对查询字符串值进行排序以便正确比较 URI:(H/T 到 Jacob 的回答)

https://dotnetfiddle.net/eEhkNk

这会保留重复键:

public static string[] QueryStringOmissions = new string[] { "b" };

public static NameValueCollection SortAndRemove(NameValueCollection collection)
{
    var orderedKeys = collection.Cast<string>().Where(k => k != null).OrderBy(k => k);
    var newCollection = HttpUtility.ParseQueryString(String.Empty);
    foreach(var key in orderedKeys)
    {
        if (!QueryStringOmissions.Contains(key))
        {
            foreach(var val in collection.GetValues(key).Select(x => x).OrderBy(x => x).ToArray())
            {
                newCollection.Add(key, val);
            }
        }
    }
    return newCollection;
}