<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://msmvps.com/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>/bill's House O Insomnia&lt;img src="http://www.williamgryan.com/images/originalcuckoo.jpg" alt="Bill Ryan" /&gt; : Explicit Range Variable</title><link>http://msmvps.com/blogs/williamryan/archive/tags/Explicit+Range+Variable/default.aspx</link><description>Tags: Explicit Range Variable</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>LINQ Gotcha</title><link>http://msmvps.com/blogs/williamryan/archive/2008/05/07/linq-gotcha.aspx</link><pubDate>Thu, 08 May 2008 00:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1616777</guid><dc:creator>William</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/williamryan/rsscomments.aspx?PostID=1616777</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/williamryan/commentapi.aspx?PostID=1616777</wfw:comment><comments>http://msmvps.com/blogs/williamryan/archive/2008/05/07/linq-gotcha.aspx#comments</comments><description>&lt;p&gt;After a long hiatus, I was trying to figure out something cool or at least interesting to blog about.&amp;nbsp; I was having trouble figuring out what I wanted to write, but then one after another, I got a ton of ideas.&lt;/p&gt;
&lt;p&gt;One thing I wanted to do for my current project was finding out if a specific ConnectionString was present in the &amp;lt;ConnectionStrings&amp;gt; setting of a .config file. This is a big oversimplification but it&amp;#39;s close enough to explain. So I wrote the following:&lt;/p&gt;&lt;code&gt;&lt;font color="#0000ff"&gt;var&lt;/font&gt; myVar = from cs in &lt;font color="#339966"&gt;ConfigurationManager&lt;/font&gt;.ConnectionStrings&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color="#0000ff"&gt;select&lt;/font&gt; cs;&lt;/code&gt;&lt;code&gt; 
&lt;p&gt;&lt;font color="#0000ff"&gt;&amp;nbsp;foreach&lt;/font&gt; (&lt;font color="#0000ff"&gt;var&lt;/font&gt; Mine &lt;font color="#0000ff"&gt;in&lt;/font&gt; myVar)&lt;br /&gt;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color="#339966"&gt;Console&lt;/font&gt;.WriteLine(myVar);&lt;br /&gt;&amp;nbsp;}&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;I knew the second I tried to write a where clause that there was a problem b/c intellisense wasn&amp;#39;t showing anything I expected. When I tried to display some of the properties in the foreach loop, nothing was visible. I knew something was wrong. So I tried to compile and got the following error:&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;Could not find an implementation of the query pattern for source type &amp;#39;System.Configuration.ConnectionStringSettingsCollection&amp;#39;.&amp;nbsp; &amp;#39;Select&amp;#39; not found.&amp;nbsp; Consider explicitly specifying the type of the range variable &amp;#39;cs&amp;#39;.&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;Well, that was helpful b/c I knew at least the problem was with the&amp;nbsp; &lt;a href="http://msdn.microsoft.com/en-us/library/bb383978.aspx"&gt;range variable&lt;/a&gt;&amp;nbsp;.&amp;nbsp; I wasn&amp;#39;t sure what type of collection the &lt;a href="http://msdn.microsoft.com/en-us/library/system.configuration.connectionstringssection.connectionstrings(VS.85).aspx"&gt;ConfigurationManager.ConnectionStrings&lt;/a&gt;&amp;nbsp; property was so I decided to look it up just to be sure. (I also just wanted to make sure it was in fact some type of collection or another. I couldn&amp;#39;t imagine that it wouldn&amp;#39;t have been,&amp;nbsp;but just wanted to make sure for purely superstitious reasons)&amp;nbsp;Ok, so it&amp;#39;s a &lt;a href="http://msdn.microsoft.com/en-us/library/system.configuration.connectionstringsettingscollection(VS.85).aspx"&gt;ConnectionStringsSettingsCollection&lt;/a&gt;&amp;nbsp;, nothing surprising there. Digging deeper I saw that &lt;a href="http://msdn.microsoft.com/en-us/library/system.configuration.connectionstringsettingscollection(VS.85).aspx"&gt;ConnectionStringsSettingsCollection&lt;/a&gt;&amp;nbsp;inherits from &lt;a href="http://msdn.microsoft.com/en-us/library/system.configuration.configurationelementcollection.aspx"&gt;ConfigurationElementCollection&lt;/a&gt;&amp;nbsp;. At that point, it all became crystal clear for I saw the following in the class definition:&lt;/p&gt;&lt;code&gt;&lt;pre class="libCScode" id="ctl00_rs1_mainContentContainer_ctl13CSharp"&gt;&lt;span style="COLOR:blue;"&gt;public&lt;/span&gt; abstract &lt;span style="COLOR:blue;"&gt;class&lt;/span&gt; ConfigurationElementCollection : ConfigurationElement, ICollection, 
    IEnumerable&lt;/code&gt;
&lt;/pre&gt;
&lt;p&gt;Do you see the problem? There&amp;#39;s an &lt;a href="http://msdn.microsoft.com/en-us/library/system.collections.ienumerable.aspx"&gt;IEnumerable&lt;/a&gt;&amp;nbsp;but no &lt;a href="http://msdn.microsoft.com/en-us/library/9eekhta0.aspx"&gt;IEnumerable&amp;lt;T&amp;gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;. You see, the collection implements the non-Generic IEnumerable but not the Generic IEnumerable.&amp;nbsp;Hence, a explicit range variable is in order to make this work.&amp;nbsp; I know I know, that&amp;#39;s exactly what the error message recommended, but I didn&amp;#39;t understand why at first and wanted to dig deeper into it. So bascially, here&amp;#39;s what was needed to make it work.&amp;nbsp; Simply use the &lt;a href="http://msdn.microsoft.com/en-us/library/bb341406.aspx"&gt;Cast&amp;lt;T&amp;gt; &lt;/a&gt;&amp;nbsp; extension method on the collection and well, that&amp;#39;s it.&lt;/p&gt;&lt;code&gt;
&lt;p&gt;&lt;font color="#0000ff"&gt;ConnectionStringSettingsCollection&lt;/font&gt; cfg = &lt;font color="#339966"&gt;ConfigurationManager&lt;/font&gt;.ConnectionStrings;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#0000ff"&gt;&lt;code&gt;var&lt;/font&gt; myvar = &lt;font color="#0000ff"&gt;from&lt;/font&gt; cs &lt;font color="#0000ff"&gt;in &lt;/font&gt;cfg.Cast&amp;lt;&lt;font color="#339966"&gt;ConnectionStringSettings&lt;/font&gt;&amp;gt;()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color="#0000ff"&gt;select&lt;/font&gt; cs;&lt;br /&gt;&lt;font color="#0000ff"&gt;foreach&lt;/font&gt; (var mine &lt;font color="#0000ff"&gt;in&lt;/font&gt; myvar)&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp; &lt;font color="#339966"&gt;&amp;nbsp;Console&lt;/font&gt;.WriteLine(mine);&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Of course, you could just cut out some of the bloat and address the collection directly&lt;/p&gt;
&lt;p&gt;&lt;code&gt;&lt;font color="#0000ff"&gt;var&lt;/font&gt; myvar = &lt;font color="#0000ff"&gt;from&lt;/font&gt; cs &lt;font color="#0000ff"&gt;in&lt;/font&gt; &lt;font color="#339966"&gt;ConfigurationManager&lt;/font&gt;.ConnectionStrings.Cast&amp;lt;&lt;font color="#339966"&gt;ConnectionStringSettings&lt;/font&gt;&amp;gt;()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color="#0000ff"&gt;select&lt;/font&gt; cs;&lt;/code&gt;&lt;br /&gt;&lt;code&gt;&lt;font color="#0000ff"&gt;foreach&lt;/font&gt; (&lt;font color="#0000ff"&gt;var&lt;/font&gt; mine &lt;font color="#0000ff"&gt;in&lt;/font&gt; myvar)&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;Console.WriteLine(mine);&lt;br /&gt;}&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Whenever Intellisense doesn&amp;#39;t do what you&amp;#39;re expecting it to, that&amp;#39;s the first tipoff something is wrong in your query.&amp;nbsp; Although my days of 2 day work weeks have come to an end temporarily, I&amp;#39;ll try to find some time to blog the rest of what I was working on - there&amp;#39;s so much you can do with LINQ that I often overlook.&amp;nbsp; And this is the first time I&amp;#39;ve come across the explicit range variable issue but I&amp;#39;m guessing it&amp;#39;ll come up again, particularly if you use LINQ regularly.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1616777" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/williamryan/archive/tags/LINQ/default.aspx">LINQ</category><category domain="http://msmvps.com/blogs/williamryan/archive/tags/Range+Variable/default.aspx">Range Variable</category><category domain="http://msmvps.com/blogs/williamryan/archive/tags/Explicit+Range+Variable/default.aspx">Explicit Range Variable</category><category domain="http://msmvps.com/blogs/williamryan/archive/tags/C_2300_+3.5/default.aspx">C# 3.5</category></item></channel></rss>