0

namevaluecollection(アイテムは通常5から15)を列挙型(60を超えるアイテムを持つ)に対してソートする必要があります。現在、私はこの拡張関数を使用しています。誰でもこのコードを書く方がよいでしょう。

    public static NameValueCollection Sort(this NameValueCollection queryString, Type orderByEnumType, bool excludeZeroValues)
    {
        NameValueCollection _processedQueryString = HttpUtility.ParseQueryString("");
        if (queryString.HasKeys())
        {
            SortedList<int, KeyValuePair<string, string>> querySortedList = new SortedList<int, KeyValuePair<string, string>>();
            string[] enumKeys = Enum.GetNames(orderByEnumType);
            int counter = 1000;
            foreach (string key in queryString)
            {
                string value = queryString[key];
                if (enumKeys.Contains(key, StringComparer.CurrentCultureIgnoreCase))
                {
                    int order = (int)Enum.Parse(orderByEnumType, key, true);
                    querySortedList.Add(order, new KeyValuePair<string, string>(key, value));
                }
                else
                {
                    querySortedList.Add(counter, new KeyValuePair<string, string>(key, value));
                    counter++;
                }
            }
            foreach (KeyValuePair<int, KeyValuePair<string, string>> kvp in querySortedList)
            {
                if (!kvp.Value.Value.IsNullOrEmpty() && !kvp.Value.Key.IsNullOrEmpty())
                {
                    if (!excludeZeroValues || kvp.Value.Value != "0")
                    {
                        _processedQueryString.Add(kvp.Value.Key, System.Web.HttpUtility.UrlEncode(kvp.Value.Value));
                    }
                }
            }
        }
        return _processedQueryString;
    }

これはこのように機能します

    public enum OrderEnum
    {
        key1=1,
        key2=20,
        key3=3,
        //note
        key4=100,
        key5=2,
        key6=6,
        key7,
        key8,
        key9 
    }
    public void Test()
    {
        NameValueCollection col1 = new NameValueCollection();
        col1.Add("key1", "value1");
        col1.Add("key9", "value1");
        col1.Add("key3", "value1");
        col1.Add("key5", "value1");
        Response.Write(col1.Sort(typeof(OrderEnum)).ToString());
        //out put: key1=value1&key5=value1&key3=value1&key9=value1
    }

これも機能するはずです

public void Test2()
    {
        NameValueCollection col1 = new NameValueCollection();
        col1.Add("key1", "value1");
        col1.Add("key-x", "value1");
        col1.Add("key-y", "value1");
        col1.Add("key9", "value1");
        col1.Add("key3", "value1");
        col1.Add("key5", "value1");
        col1.Add("key-z", "value1");
        Response.Write(col1.Sort(typeof(OrderEnum)).ToString());
        //out put: key1=value1&key5=value1&key3=value1&key9=value1&key-x=value1&key-y=value1&key-z=value1
    }
4

3 に答える 3

1

namevaluecollectionをkeyvaluepairsのリストに変換し、操作ごとに単純なLINQ順序を適用する方がよいと思います。これは、すばやく簡単です。

新しい拡張メソッドを追加して、namevaluecollをキーと値のペアのリストに変換します

    public static List<KeyValuePair<string, string>> ToPairs(this System.Collections.Specialized.NameValueCollection collection)
    {
        if (collection == null)
        {
            throw new ArgumentNullException("collection");
        }

        return collection.Cast<string>().Select(key => new KeyValuePair<string, string>(key, collection[key])).ToList();
    } 

そして、このオブジェクトにlinq順序を適用するだけで、次のようになります。

        System.Collections.Specialized.NameValueCollection col1= new System.Collections.Specialized.NameValueCollection();
        col1.Add("key1", "value1");
        col1.Add("key-x", "value2");
        col1.Add("key-y", "value3");
        col1.Add("key9", "value4");
        col1.Add("key3", "value5");
        col1.Add("key5", "value6");
        col1.Add("key-z", "value7"); 

        var nvc = col1.ToPairs();

        // To order the items based on key in descending order
        var orderedbykey=nvc.OrderByDescending(x => x.Key).ToList();  

       // To order the items based on value in descending order           
       var orderedbyval=nvc.OrderByDescending(x => x.Value).ToList();

       //or order by ur custom enum key
        var orderbyEnumKeys = colc.OrderBy(x =>
        {
            int en;
            try
            {
                 en = (int)Enum.Parse(typeof(OrderEnum), x.Key);
            }
            catch (Exception ex)
            {
                return int.MaxValue;
            }
            return en;
        }).ToList();

お役に立てれば..

于 2010-02-23T09:28:44.050 に答える
0

あなたはそのようなことをすることができます:

public static NameValueCollection SortByEnum<TEnum>(this NameValueCollection source) where TEnum : struct
{
    var orderedKeys = source.Keys.Cast<string>().OrderBy(k => Enum.IsDefined(typeof(TEnum), k) ? Convert.ToInt32(Enum.Parse(typeof(TEnum), k)) : int.MaxValue);
    var ordered = new NameValueCollection();
    foreach(var key in orderedKeys) ordered.Add(key, source[key]);
    return ordered;
}
于 2010-02-23T09:33:18.047 に答える
0

私はついにこの解決策で結論に達しました

public static NameValueCollection SortByEnum<TEnum>(this NameValueCollection source) where TEnum : struct
{
    var orderedKeys = source.Keys.Cast<string>().OrderBy(k => ((Enum.IsDefined(typeof(TEnum), k)) ? ((int)Enum.Parse(typeof(TEnum), k)) : int.MaxValue));
    var ordered = HttpUtility.ParseQueryString("");
    foreach (var key in orderedKeys)
    {
        ordered.Add(key, source[key]);
    }
    return ordered;
}

これですべての問題が解決しますありがとう@thomas@ramesh

于 2010-02-23T11:04:33.580 に答える