<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" 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:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>IIrrelevant - LINQ</title>
    <link>http://www.milkcarton.com/blog/</link>
    <description>Irrelevant musings about software development</description>
    <language>en-us</language>
    <copyright>Dan Morphis</copyright>
    <lastBuildDate>Fri, 09 May 2008 09:13:08 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.3.9074.18820</generator>
    <managingEditor>blog@milkcarton.com</managingEditor>
    <webMaster>blog@milkcarton.com</webMaster>
    <item>
      <trackback:ping>http://www.milkcarton.com/blog/Trackback.aspx?guid=65541c8f-4900-4677-bec2-557da84e1ecb</trackback:ping>
      <pingback:server>http://www.milkcarton.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.milkcarton.com/blog/PermaLink,guid,65541c8f-4900-4677-bec2-557da84e1ecb.aspx</pingback:target>
      <dc:creator>Dan Morphis</dc:creator>
      <wfw:comment>http://www.milkcarton.com/blog/CommentView,guid,65541c8f-4900-4677-bec2-557da84e1ecb.aspx</wfw:comment>
      <wfw:commentRss>http://www.milkcarton.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=65541c8f-4900-4677-bec2-557da84e1ecb</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <h3>The Problem
</h3>
        <p>
For the last few days I've been trying to figure out how to do this SQL in linq:
</p>
        <div>
          <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
              <span style="color: #606060"> 1:</span>
              <span style="color: #0000ff">select</span>
              <span style="color: #0000ff">from</span> customers <span style="color: #0000ff">where</span><span style="color: #0000ff">substring</span>(customers.Name,
1, 1) <span style="color: #0000ff">IN</span> (<span style="color: #006080">'a'</span>, <span style="color: #006080">'b'</span>, <span style="color: #006080">'c'</span>, <span style="color: #006080">'d'</span>);</pre>
          </div>
The problem is the list (a, b, c, d) is variable, on one page I need all the results
where foo.Name starts with a or b, on another it could be b, c, d, e and f. 
This is trivial to do in SQL, you just break out a little dynamic sql and your done. 
But being a noob to LINQ, I'm not so sure how to go about doing this.
</div>
        <p>
I had a lot of failed queries, and failed code, before giving up and just looping
through the letters for that particular page and manually building up what I wanted.
</p>
        <p>
But that's kind of a hack. OK, its not kind of a hack, its a big hack.
</p>
        <h3>The Solution
</h3>
        <p>
This LINQ goodness (using the Customers db in <a href="http://www.linqpad.net/">LINQPad</a>)
will pull back all the customers who's names begin with a t, d, or j (note the .ToList()
on line 4, I'll discuss it next).
</p>
        <div>
          <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
              <span style="color: #606060"> 1:</span>
              <span style="color: #0000ff">string</span>[]
letters = { <span style="color: #006080">"t"</span>, <span style="color: #006080">"d"</span>, <span style="color: #006080">"j"</span> }; </pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
              <span style="color: #606060"> 2:</span>  </pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
              <span style="color: #606060"> 3:</span> var
customers = (from c <span style="color: #0000ff">in</span> Customers</pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
              <span style="color: #606060"> 4:</span> select
c).ToList(); </pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
              <span style="color: #606060"> 5:</span>  </pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
              <span style="color: #606060"> 6:</span> var
results = from c <span style="color: #0000ff">in</span> customers</pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
              <span style="color: #606060"> 7:</span> join
letter <span style="color: #0000ff">in</span> letters on c.Name.First().ToString().ToUpper()
equals letter.ToUpper()</pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
              <span style="color: #606060"> 8:</span> select
c; </pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
              <span style="color: #606060"> 9:</span>  </pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
              <span style="color: #606060"> 10:</span> customers.Dump(<span style="color: #006080">"Customers"</span>);</pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
              <span style="color: #606060"> 11:</span> results.Dump(<span style="color: #006080">"Filtered
by first letter"</span>);</pre>
          </div>
        </div>
        <p>
And LINQPad renders this for us:
</p>
        <p>
          <a href="http://www.milkcarton.com/blog/content/binary/WindowsLiveWriter/LINQGoodness_E90/image_2.png">
            <img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="244" alt="image" src="http://www.milkcarton.com/blog/content/binary/WindowsLiveWriter/LINQGoodness_E90/image_thumb.png" width="176" border="0" />
          </a>
        </p>
        <h3>
Limitations
</h3>
        <p>
LINQ to SQL doesn't support querying a SQL database with if one of your sources is
an in-memory store, except if you use the Contains operator.  So, to work around
that, you have to pull back ALL the results from the db, and then convert it to a
List before you use it in the above query.
</p>
        <p>
I'm my opinion, this is kind of a painful limitation, but for my particular purpose,
I'm willing to live with it because my particular database will only ever have around
100 or so rows, so pulling everything back in memory isn't such a big deal...
</p>
        <p>
P.S. if you are reading this in a feed reader, let me know how it renders, I've updated
my code snippet plugin to try and fix the previous rendering problems.
</p>
        <img width="0" height="0" src="http://www.milkcarton.com/blog/aggbug.ashx?id=65541c8f-4900-4677-bec2-557da84e1ecb" />
      </body>
      <title>LINQ Goodness</title>
      <guid isPermaLink="false">http://www.milkcarton.com/blog/PermaLink,guid,65541c8f-4900-4677-bec2-557da84e1ecb.aspx</guid>
      <link>http://www.milkcarton.com/blog/2008/05/09/LINQ+Goodness.aspx</link>
      <pubDate>Fri, 09 May 2008 09:13:08 GMT</pubDate>
      <description>&lt;h3&gt;The Problem
&lt;/h3&gt;
&lt;p&gt;
For the last few days I've been trying to figure out how to do this SQL in linq:
&lt;/p&gt;
&lt;div&gt;
&lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 1:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;select&lt;/span&gt; &lt;span style="color: #0000ff"&gt;from&lt;/span&gt; customers &lt;span style="color: #0000ff"&gt;where&lt;/span&gt; &lt;span style="color: #0000ff"&gt;substring&lt;/span&gt;(customers.Name,
1, 1) &lt;span style="color: #0000ff"&gt;IN&lt;/span&gt; (&lt;span style="color: #006080"&gt;'a'&lt;/span&gt;, &lt;span style="color: #006080"&gt;'b'&lt;/span&gt;, &lt;span style="color: #006080"&gt;'c'&lt;/span&gt;, &lt;span style="color: #006080"&gt;'d'&lt;/span&gt;);&lt;/pre&gt;
&lt;/div&gt;
The problem is the list (a, b, c, d) is variable, on one page I need all the results
where foo.Name starts with a or b, on another it could be b, c, d, e and f.&amp;nbsp;
This is trivial to do in SQL, you just break out a little dynamic sql and your done.&amp;nbsp;
But being a noob to LINQ, I'm not so sure how to go about doing this.
&lt;/div&gt;
&lt;p&gt;
I had a lot of failed queries, and failed code, before giving up and just looping
through the letters for that particular page and manually building up what I wanted.
&lt;/p&gt;
&lt;p&gt;
But that's kind of a hack. OK, its not kind of a hack, its a big hack.
&lt;/p&gt;
&lt;h3&gt;The Solution
&lt;/h3&gt;
&lt;p&gt;
This LINQ goodness (using the Customers db in &lt;a href="http://www.linqpad.net/"&gt;LINQPad&lt;/a&gt;)
will pull back all the customers who's names begin with a t, d, or j (note the .ToList()
on line 4, I'll discuss it next).
&lt;/p&gt;
&lt;div&gt;
&lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 1:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt;[]
letters = { &lt;span style="color: #006080"&gt;"t"&lt;/span&gt;, &lt;span style="color: #006080"&gt;"d"&lt;/span&gt;, &lt;span style="color: #006080"&gt;"j"&lt;/span&gt; }; &lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 2:&lt;/span&gt;&amp;nbsp; &lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 3:&lt;/span&gt; var
customers = (from c &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; Customers&lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 4:&lt;/span&gt; select
c).ToList(); &lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 5:&lt;/span&gt;&amp;nbsp; &lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 6:&lt;/span&gt; var
results = from c &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; customers&lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 7:&lt;/span&gt; join
letter &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; letters on c.Name.First().ToString().ToUpper()
equals letter.ToUpper()&lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 8:&lt;/span&gt; select
c; &lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 9:&lt;/span&gt;&amp;nbsp; &lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 10:&lt;/span&gt; customers.Dump(&lt;span style="color: #006080"&gt;"Customers"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 11:&lt;/span&gt; results.Dump(&lt;span style="color: #006080"&gt;"Filtered
by first letter"&lt;/span&gt;);&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
And LINQPad renders this for us:
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.milkcarton.com/blog/content/binary/WindowsLiveWriter/LINQGoodness_E90/image_2.png"&gt;&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="244" alt="image" src="http://www.milkcarton.com/blog/content/binary/WindowsLiveWriter/LINQGoodness_E90/image_thumb.png" width="176" border="0"&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;h3&gt;
Limitations
&lt;/h3&gt;
&lt;p&gt;
LINQ to SQL doesn't support querying a SQL database with if one of your sources is
an in-memory store, except if you use the Contains operator.&amp;nbsp; So, to work around
that, you have to pull back ALL the results from the db, and then convert it to a
List before you use it in the above query.
&lt;/p&gt;
&lt;p&gt;
I'm my opinion, this is kind of a painful limitation, but for my particular purpose,
I'm willing to live with it because my particular database will only ever have around
100 or so rows, so pulling everything back in memory isn't such a big deal...
&lt;/p&gt;
&lt;p&gt;
P.S. if you are reading this in a feed reader, let me know how it renders, I've updated
my code snippet plugin to try and fix the previous rendering problems.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.milkcarton.com/blog/aggbug.ashx?id=65541c8f-4900-4677-bec2-557da84e1ecb" /&gt;</description>
      <comments>http://www.milkcarton.com/blog/CommentView,guid,65541c8f-4900-4677-bec2-557da84e1ecb.aspx</comments>
      <category>.NET</category>
      <category>C#</category>
      <category>LINQ</category>
    </item>
  </channel>
</rss>