<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>.NET</title>
        <link>http://geekswithblogs.net/plundberg/category/9629.aspx</link>
        <description>.NET</description>
        <language>sv-FI</language>
        <copyright>plundberg</copyright>
        <managingEditor>per.lundberg@ecraft.com</managingEditor>
        <generator>Subtext Version 0.0.0.0</generator>
        <item>
            <title>Instantiating a Dictionary from a LINQ projection, part 2</title>
            <link>http://geekswithblogs.net/plundberg/archive/2009/04/29/instantiating-a-dictionary-from-a-linq-projection-part-2.aspx</link>
            <description>(This posting is a continuation of &lt;a href="javascript:void(0);/*1240987479017*/"&gt;this&lt;/a&gt; blog posting.)&lt;br /&gt;
&lt;br /&gt;
If you want to implement this the OOP way, here you have the subclass code (which I'll use myself).&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New;"&gt;using System.Collections.Generic;&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;// Remember to set this to your proper namespace (or use a using statement in&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;// the class with the LINQ projection)&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;namespace MyNamespace&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;{&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;    /// &amp;lt;summary&amp;gt;&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;    /// Extension methods for the Dictionary generic class.&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;    /// &amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;    public class ImprovedDictionary&amp;lt;T1,T2&amp;gt;: Dictionary&amp;lt;T1,T2&amp;gt;&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;    {&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;        /// &amp;lt;summary&amp;gt;&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;        /// Add a key/value pair to the dictionary and return the dictionary reference.&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;        /// &amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;        /// &amp;lt;param name="key"&amp;gt;the key&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;        /// &amp;lt;param name="value"&amp;gt;the value&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;        /// &amp;lt;returns&amp;gt;the dictionary&amp;lt;/returns&amp;gt;&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;        public &lt;/span&gt;&lt;span style="font-family: Courier New;"&gt;ImprovedDictionary&lt;/span&gt;&lt;span style="font-family: Courier New;"&gt;&amp;lt;T1, T2&amp;gt; AddWithReturn(T1 key, T2 value)&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;        {&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;            this[key] = value;&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;            return this;&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;        }&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;    }&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;}&lt;/span&gt; &lt;img src="http://geekswithblogs.net/plundberg/aggbug/131563.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>plundberg</dc:creator>
            <guid>http://geekswithblogs.net/plundberg/archive/2009/04/29/instantiating-a-dictionary-from-a-linq-projection-part-2.aspx</guid>
            <pubDate>Wed, 29 Apr 2009 14:43:40 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/plundberg/comments/131563.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/plundberg/archive/2009/04/29/instantiating-a-dictionary-from-a-linq-projection-part-2.aspx#feedback</comments>
            <slash:comments>1</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/plundberg/comments/commentRss/131563.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Instantiating a Dictionary from a LINQ projection</title>
            <link>http://geekswithblogs.net/plundberg/archive/2009/04/29/instantiating-a-dictionary-from-a-linq-projection.aspx</link>
            <description>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:&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;var result = from o in ctx.Orders&lt;br /&gt;             where o.ID = OrderID&lt;br /&gt;             select new MyOrderClass&lt;br /&gt;             {&lt;br /&gt;                 ID = o.ID,&lt;br /&gt;                 Created = o.Created,&lt;br /&gt;                 ExtraData = new Dictionary&amp;lt;string,string&amp;gt;().&lt;br /&gt;                     Add("key1", o.Value1).&lt;br /&gt;                     Add("key2", o.Value2")&lt;br /&gt;             };&lt;/pre&gt;
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.&lt;br /&gt;
&lt;br /&gt;
So, what do we do? If you've done your C# 3.0 homework, the answer should be fairly obvious: An extension method!&lt;br /&gt;
&lt;br /&gt;
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 &lt;span style="font-style: italic;"&gt;as if&lt;/span&gt; they were a part of the original class.&lt;br /&gt;
&lt;br /&gt;
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&amp;lt;string,string&amp;gt;, 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. :-)&lt;br /&gt;
&lt;br /&gt;
Create a new class, with this content:&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;using System.Collections.Generic;&lt;br /&gt;&lt;br /&gt;// Make sure the namespace below is the same for both your extension method class and the class &lt;br /&gt;// where you are writing the LINQ projection, or use a using statement in the latter one.&lt;br /&gt;namespace MyNamespace&lt;br /&gt;{&lt;br /&gt;    /// &amp;lt;summary&amp;gt;&lt;br /&gt;    /// Extension methods for the Dictionary generic class.&lt;br /&gt;    /// &amp;lt;/summary&amp;gt;&lt;br /&gt;    public static class DictionaryExtension&lt;br /&gt;    {&lt;br /&gt;        /// &amp;lt;summary&amp;gt;&lt;br /&gt;        /// Add a key/value pair to the dictionary and return the dictionary reference.&lt;br /&gt;        /// &amp;lt;/summary&amp;gt;&lt;br /&gt;        /// &amp;lt;param name="dictionary"&amp;gt;the dictionary to add the key/value pair to&amp;lt;/param&amp;gt;&lt;br /&gt;        /// &amp;lt;param name="key"&amp;gt;the key&amp;lt;/param&amp;gt;&lt;br /&gt;        /// &amp;lt;param name="value"&amp;gt;the value&amp;lt;/param&amp;gt;&lt;br /&gt;        /// &amp;lt;returns&amp;gt;the dictionary&amp;lt;/returns&amp;gt;&lt;br /&gt;        public static Dictionary&amp;lt;string, string&amp;gt; AddWithReturn(this Dictionary&amp;lt;string, string&amp;gt; dictionary, string key, string value)&lt;br /&gt;        {&lt;br /&gt;            dictionary[key] = value;&lt;br /&gt;            return dictionary;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;
&lt;br /&gt;
That's it. Now, change the above LINQ query to this:&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;var result = from o in ctx.Orders&lt;br /&gt;             where o.ID = OrderID&lt;br /&gt;             select new MyOrderClass&lt;br /&gt;             {&lt;br /&gt;                 ID = o.ID,&lt;br /&gt;                 Created = o.Created,&lt;br /&gt;                 ExtraData = new Dictionary&amp;lt;string,string&amp;gt;().&lt;br /&gt;                     AddWithReturn("key1", o.Value1).&lt;br /&gt;                     AddWithReturn("key2", o.Value2")&lt;br /&gt;             };&lt;/pre&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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). &lt;img src="http://geekswithblogs.net/plundberg/aggbug/131562.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>plundberg</dc:creator>
            <guid>http://geekswithblogs.net/plundberg/archive/2009/04/29/instantiating-a-dictionary-from-a-linq-projection.aspx</guid>
            <pubDate>Wed, 29 Apr 2009 14:37:36 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/plundberg/comments/131562.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/plundberg/archive/2009/04/29/instantiating-a-dictionary-from-a-linq-projection.aspx#feedback</comments>
            <slash:comments>2</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/plundberg/comments/commentRss/131562.aspx</wfw:commentRss>
        </item>
    </channel>
</rss>
