<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://weblogs.asp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Multi-value Dictionary C# source code (.NET 3.5)</title><link>http://weblogs.asp.net/fbouma/archive/2009/05/18/multi-value-dictionary-c-source-code-net-3-5.aspx</link><description>By popular demand, I've published the C# source code of my Multi-value Dictionary class, which can also merge dictionaries into itself and which implements ILookup&amp;lt;T, V&amp;gt; as well. It's part of Algorithmia, our upcoming data-structure and algorithm</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>re: Multi-value Dictionary C# source code (.NET 3.5)</title><link>http://weblogs.asp.net/fbouma/archive/2009/05/18/multi-value-dictionary-c-source-code-net-3-5.aspx#7098020</link><pubDate>Tue, 26 May 2009 13:12:33 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7098020</guid><dc:creator>FransBouma</dc:creator><author>FransBouma</author><description>&lt;p&gt;@Steve: in the relational world, it can be very handy :) How about you have a list of Order objects, and you want to store all orders under their customerid FK field value? With this dictionary you can do that: the customerid field is the key, the values are the order instances, which allows you to quickly find the Order instances for a given customerID in memory using the dictionary without needing to traverse all orders sequentially &lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7098020" width="1" height="1"&gt;</description></item><item><title>re: Multi-value Dictionary C# source code (.NET 3.5)</title><link>http://weblogs.asp.net/fbouma/archive/2009/05/18/multi-value-dictionary-c-source-code-net-3-5.aspx#7098007</link><pubDate>Tue, 26 May 2009 12:34:07 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7098007</guid><dc:creator>Steve Raynolds</dc:creator><author>Steve Raynolds</author><description>&lt;p&gt;a newbie and stupid question, but why do I need it?&lt;/p&gt;
&lt;p&gt;e.g. As I understand it is for storing values with 2 different keys.&lt;/p&gt;
&lt;p&gt;How would I connect it to a relational world, e.g. what to store in such a collection? Sometimes your articles are hard to read for not-godlike coders like you ;p&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7098007" width="1" height="1"&gt;</description></item><item><title>re: Multi-value Dictionary C# source code (.NET 3.5)</title><link>http://weblogs.asp.net/fbouma/archive/2009/05/18/multi-value-dictionary-c-source-code-net-3-5.aspx#7093619</link><pubDate>Wed, 20 May 2009 15:25:44 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7093619</guid><dc:creator>FransBouma</dc:creator><author>FransBouma</author><description>&lt;p&gt;@Mark: I think the compiler simply checks whether a type implements IEnumerable&amp;lt;T&amp;gt; and as the type implements that interface twice (once as the Dictionary&amp;lt;&amp;gt; and once as ILookup&amp;lt;&amp;gt;), it gets confused. As ILookup seems to be some kind of specific linq query interface, I think the scope for the interface is smaller than the one it is used in with this class. &lt;/p&gt;
&lt;p&gt;the GetValues is indeed a 'workaround', though thinking about ILookup some more, I think it shouldn't be on this dictionary, it doesn't really add anything. &lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7093619" width="1" height="1"&gt;</description></item><item><title>re: Multi-value Dictionary C# source code (.NET 3.5)</title><link>http://weblogs.asp.net/fbouma/archive/2009/05/18/multi-value-dictionary-c-source-code-net-3-5.aspx#7093451</link><pubDate>Wed, 20 May 2009 10:22:34 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7093451</guid><dc:creator>Mark Smeltzer</dc:creator><author>Mark Smeltzer</author><description>&lt;p&gt;Wow. You&amp;#39;re right. I just brewed up a test class that implements IEnumerable&amp;lt;string&amp;gt; and explicitly implements ILookup&amp;lt;string,string&amp;gt;.&lt;/p&gt;
&lt;p&gt;While intellisense correctly shows the overloads for the extension methods of associated with the IEnumerable&amp;lt;string&amp;gt; interface, it appears that the parser/compiler gets confused about how to interpret the various extension methods that are applicable to the explicitly defined ILookup&amp;lt;string, string&amp;gt; interface.&lt;/p&gt;
&lt;p&gt;I had (incorrectly) assumed that Microsoft made the parser/compiler mirror the functionality of the intellisense drop down. That seems like a design flaw to me...&lt;/p&gt;
&lt;p&gt;So, yeah, I guess the only thing left is something like this: add a property called &amp;quot;Values&amp;quot; (or whatever seems appropriate) that returns an IEnumerable&amp;lt;T&amp;gt;, which iterates over the same enumeration that the IEnumerable&amp;lt;T&amp;gt; implementation that the dictionary&amp;#39;s IEnumerable&amp;lt;T&amp;gt; implementation iterates over. That could be implemented as a simple redirection, but it would make both the compiler and intellisense happy. &lt;/p&gt;
&lt;p&gt;That way the user can do something like this:&lt;/p&gt;
&lt;p&gt; &amp;nbsp; var dictionary = GetMultiValueDictionary();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; dictionary.Values.Select( t=&amp;gt; t.Length ).Where( t=&amp;gt; t &amp;gt; 0 );&lt;/p&gt;
&lt;p&gt;This could even be implemented externally as an extension method instead of as a property on the original class:&lt;/p&gt;
&lt;p&gt; &amp;nbsp; var dictionary = GetMultiValueDictionary();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; dictionary.GetValues().Select( t=&amp;gt; t.Length ).Where( t=&amp;gt; t &amp;gt; 0 );&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7093451" width="1" height="1"&gt;</description></item><item><title>re: Multi-value Dictionary C# source code (.NET 3.5)</title><link>http://weblogs.asp.net/fbouma/archive/2009/05/18/multi-value-dictionary-c-source-code-net-3-5.aspx#7093432</link><pubDate>Wed, 20 May 2009 09:50:46 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7093432</guid><dc:creator>FransBouma</dc:creator><author>FransBouma</author><description>&lt;p&gt;@Mark: I made it explicit, that's the point: a Linq to objects enumerable method like Where also works with ILookup&amp;lt;T, U&amp;gt;.IEnumerable&amp;lt;IGrouping&amp;lt;T, U&amp;gt;&amp;gt;, which makes it problematic because there are now two enumerables and which one to choose by the compiler? so the compiler wants to explicitly define the generic parameters on the linq extension methods, which is awkward. &lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7093432" width="1" height="1"&gt;</description></item><item><title>re: Multi-value Dictionary C# source code (.NET 3.5)</title><link>http://weblogs.asp.net/fbouma/archive/2009/05/18/multi-value-dictionary-c-source-code-net-3-5.aspx#7093418</link><pubDate>Wed, 20 May 2009 09:27:21 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7093418</guid><dc:creator>Mark Smeltzer</dc:creator><author>Mark Smeltzer</author><description>&lt;p&gt;You can make the ILookup&amp;lt;&amp;gt; implementation explicit. That way, if anyone wants the ILookup&amp;lt;&amp;gt; functions, they can cast to ILookup&amp;lt;&amp;gt; (and thus get the appropriate extension methods). &lt;/p&gt;
&lt;p&gt;Otherwise, the default behavior would be that intellisense will show only fields (and extensions) from non-explicitly implemented interfaces.&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7093418" width="1" height="1"&gt;</description></item><item><title>re: Multi-value Dictionary C# source code (.NET 3.5)</title><link>http://weblogs.asp.net/fbouma/archive/2009/05/18/multi-value-dictionary-c-source-code-net-3-5.aspx#7093392</link><pubDate>Wed, 20 May 2009 08:38:22 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7093392</guid><dc:creator>belayet hossain</dc:creator><author>belayet hossain</author><description>&lt;p&gt;nice&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7093392" width="1" height="1"&gt;</description></item><item><title>re: Multi-value Dictionary C# source code (.NET 3.5)</title><link>http://weblogs.asp.net/fbouma/archive/2009/05/18/multi-value-dictionary-c-source-code-net-3-5.aspx#7092241</link><pubDate>Mon, 18 May 2009 19:45:13 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7092241</guid><dc:creator>FransBouma</dc:creator><author>FransBouma</author><description>&lt;p&gt;@Steven: I have no choice: the class inherits from Dictionary (implements IEnumerable&amp;lt;&amp;gt;) and ILookup also implements IEnumerable&amp;lt;&amp;gt; but different generic types. So ILookup has to change, but that's not my call (it's a BCL interface ;)). &lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7092241" width="1" height="1"&gt;</description></item><item><title>re: Multi-value Dictionary C# source code (.NET 3.5)</title><link>http://weblogs.asp.net/fbouma/archive/2009/05/18/multi-value-dictionary-c-source-code-net-3-5.aspx#7092219</link><pubDate>Mon, 18 May 2009 19:21:18 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7092219</guid><dc:creator>Steven</dc:creator><author>Steven</author><description>&lt;p&gt;&amp;gt;&amp;gt; the compiler and intellisense get confused&lt;/p&gt;
&lt;p&gt;Perhaps you need to rethink the design? :-)&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7092219" width="1" height="1"&gt;</description></item></channel></rss>