Ever needed to instantiate a Dictionary from a LINQ projection? It is certainly doable, although not extremely straightforward. My first thought was to try and do this:
var result = from o in ctx.Orders
where o.ID = OrderID
select new MyOrderClass
{
ID = o.ID,
Created = o.Created,
ExtraData = new Dictionary<string,string>().
Add("key1", o.Value1).
Add("key2", o.Value2")
};
If this would work, it would probably be the most clean-looking solution. The only problem with this though is that the Add() method in the Dictionary class doesn't return the Dictionary object itself... so unfortunately, this makes it unusable from within a LINQ projection.
So, what do we do? If you've done your C# 3.0 homework, the answer should be fairly obvious: An extension method!
Extensions method, albeit not 100% OOP-pure, are a convenient way to be able to "inject" new methods into an existing class. Of course, this is mostly "syntactic sugar". The methods aren't really inserted into the existing class, but the C# compiler lets you call the methods
as if they were a part of the original class.
When writing this blog posting, I realize that in this specific case, we don't actually have to use an extension method. We could just subclass the Dictionary<string,string>, and add our own method to this subclass. If you want to do it the OOP way, this is preferrable. On the other hand, if you want to practice your C# 3.0 skills, keep reading. :-)
Create a new class, with this content:
using System.Collections.Generic;
// Make sure the namespace below is the same for both your extension method class and the class
// where you are writing the LINQ projection, or use a using statement in the latter one.
namespace MyNamespace
{
/// <summary>
/// Extension methods for the Dictionary generic class.
/// </summary>
public static class DictionaryExtension
{
/// <summary>
/// Add a key/value pair to the dictionary and return the dictionary reference.
/// </summary>
/// <param name="dictionary">the dictionary to add the key/value pair to</param>
/// <param name="key">the key</param>
/// <param name="value">the value</param>
/// <returns>the dictionary</returns>
public static Dictionary<string, string> AddWithReturn(this Dictionary<string, string> dictionary, string key, string value)
{
dictionary[key] = value;
return dictionary;
}
}
}
That's it. Now, change the above LINQ query to this:
var result = from o in ctx.Orders
where o.ID = OrderID
select new MyOrderClass
{
ID = o.ID,
Created = o.Created,
ExtraData = new Dictionary<string,string>().
AddWithReturn("key1", o.Value1).
AddWithReturn("key2", o.Value2")
};
Convenient, huh? So, when should we use the extension method approach, and when should we adhere to the OOP standard way of doing it? Well, I'd say like this: If we need to do this on different types of dictionaries (with different key/value types), the extension method approach might be better. I'm not sure if you can subclass a generic class (and have the subclass be generic as well). Maybe you can, and if so, this might be the most elegant way to do it.
If you want to do it OOP (which you should strive for, if possible), only use the approach above when you have to (i.e. when subclassing isn't possible, because the parent class is sealed or similar).