How to sort Rad grid with List<NameValueCollection> t RRS feed

  • Question

  • User-405360281 posted

    I facing problem in sorting Radgrid datasource is List<NameValueCollection> .

    Please help me how I can sort the result.

    Monday, December 27, 2010 6:32 AM

All replies

  • User-443671128 posted

    I think its better to convert your namevaluecollection into List of keyvaluepairs and apply a simple LINQ order by operation, thats quick and simple.

    Add a new extension method to convert your namevaluecoll into list of keyvaluepairs

    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();

    And just apply the linq order by over this object, something like this

    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;
                     en = (int)Enum.Parse(typeof(OrderEnum), x.Key);
                catch (Exception ex)
                    return int.MaxValue;
                return en;

    Hope this helps to you................

    Tuesday, December 28, 2010 1:34 AM