There is no SortedDictionary in Silverlight… so what?

August 14, 2009 11:39

Silverlight by its nature supports a limited subset of the .NET Framework. Recently, I ran into its lack of WPF color support. Now, I've run into another one: no SortedDictionary.

You can see this by taking a quick look at the contents of the two different System.dll's (Silverlight comes with its own). Here's the full-blown version, found at C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.dll:

image

Now, for the version of System.dll that comes with Silverlight (found at c:\Program Files\Reference Assemblies\Microsoft\Framework\Silverlight\v3.0\system.dll):

image

 

Hmmm, no SortedDictionary. That's a bummer, cause I have a situation where I really need a dictionary in sorted order.

<sidenote>You can try adding a reference to the first, full-blown System.dll, but you're indulging in an exercise of futility:

image

As you can see, such a strategy isn't going to work.</sidenote>

An Alternative to SortedDictionary, or LINQ to the rescue

So, what to do?

First, define a new class to contain key/value pairs:

   1: public class KeyValue
   2: {
   3:     public string Key { get; set; }
   4:     public string Value { get; set; }
   5: }

Then, create a list that uses KeyValue. We'll seed some sample data, too.

   1: var paramList = new List<KeyValue>
   2:     {
   3:         new KeyValue {Key = "Programming WCF", Value = "Currently Reading"},
   4:         new KeyValue {Key = "JavaScript: The Definitive Guide", Value = "Currently Reading"},
   5:         new KeyValue {Key = "Professional ASP.NET 3.5", Value = "Currently Reading"},
   6:         new KeyValue {Key = "JavaScript: The Good Parts", Value = "Next on the list"},
   7:         new KeyValue {Key = "AJAX and PHP", Value = "Eventually"},
   8:         new KeyValue {Key = "Agile Software Development", Value = "Someday"}
   9:     };

Now, the beauty of LINQ-to-Objects to sort our list:

   1: var sortedList = from q in paramList orderby q.Key ascending select q;

That's it. "sortedList" contains all of our KeyValue pairs, but in sorted order:

Key: "Agile Software Development" Value: "Someday"
Key: "AJAX and PHP" Value: "Eventually"
Key: "JavaScript: The Definitive Guide" Value: "Currently Reading"
Key: "JavaScript: The Good Parts" Value: "Next on the list"
Key: "Professional ASP.NET 3.5" Value: "Currently Reading"
Key: "Programming WCF" Value: "Currently Reading"

Here's the full code snippet:

   1: using System.Collections.Generic;
   2: using System.Diagnostics;
   3: using System.Linq;
   4:  
   5: namespace ConsoleApplication1
   6: {
   7:     public class KeyValue
   8:     {
   9:         public string Key { get; set; }
  10:         public string Value { get; set; }
  11:     }
  12:  
  13:     class Program
  14:     {
  15:         static void Main (string[] args)
  16:         {
  17:             var paramList = new List<KeyValue>
  18:                 {
  19:                     new KeyValue {Key = "Programming WCF", Value = "Currently Reading"},
  20:                     new KeyValue {Key = "JavaScript: The Definitive Guide", Value = "Currently Reading"},
  21:                     new KeyValue {Key = "Professional ASP.NET 3.5", Value = "Currently Reading"},
  22:                     new KeyValue {Key = "JavaScript: The Good Parts", Value = "Next on the list"},
  23:                     new KeyValue {Key = "AJAX and PHP", Value = "Eventually"},
  24:                     new KeyValue {Key = "Agile Software Development", Value = "Someday"}
  25:                 };
  26:  
  27:             var sortedList = from q in paramList orderby q.Key ascending select q;
  28:  
  29:             foreach (var k in sortedList)
  30:             {
  31:                 Debug.WriteLine (string.Format("Key: \"{0}\" Value: \"{1}\"", k.Key, k.Value));
  32:             }
  33:         }
  34:     }
  35: }

Comments

All comments are moderated and require approval before display. Thanks for your understanding.

Add comment


 


[b][/b] - [i][/i] - [u][/u] - [q][/q]



Preview