Per Lundberg

Random thoughts from a BizTalk developer
posts - 10, comments - 10, trackbacks - 0

My Links

News

Archives

Post Categories

Instantiating a Dictionary from a LINQ projection

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).

Print | posted on Wednesday, April 29, 2009 9:37 AM | Filed Under [ .NET C# LINQ ]

Feedback

Gravatar

# re: Instantiating a Dictionary from a LINQ projection

Hey, you're right. :-) Thank you, I think I'll change my code straight away. So, the example is not relevant in itself, but it might still help someone trying to do something similar where the extension method/OOP subclassing way is actually necessary.

Best regards,
Per
5/5/2009 10:20 AM | Per Lundberg
Gravatar

# re: Instantiating a Dictionary from a LINQ projection

Two comments:

1) Chris' idea is great, but the problem is that collection initializers don't work with LINQ to SQL projections. They cause an exception with the message "Unrecognized expression node: ListInit". I think this is because collection initializers (which probably invoke the method ListInit() behind the scene, or similar) are not supported by LINQ to SQL.

2) There is another problem with my approach. This holds true at least for the OOP version, but maybe the extension method also has this problem. The problem is that the data gets destroyed if you do it like this. The dictionary contains the same data for record #1 and record #2 in the output array, even though they might be different in the database! This is a very severe and nasty problem. The solution which I implemented is to move the Dictionary initialization into a helper method of its own. (e.g. GetExtraData()). You pass in the o object into this method, and then let that method return a new Dictionary<string,string> object. That way, it works perfectly.

Best regards,
Per
5/6/2009 9:39 AM | Per Lundberg
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

Powered by: