<?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>Coeamyd's - LINQ</title>
    <link>http://www.coeamyd.net/</link>
    <description>...It ain't easy being green</description>
    <language>en-us</language>
    <copyright>Christoph Herold</copyright>
    <lastBuildDate>Thu, 16 Jul 2009 11:04:26 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 1.9.6264.0</generator>
    <managingEditor>Christoph.Herold@coeamyd.net</managingEditor>
    <webMaster>Christoph.Herold@coeamyd.net</webMaster>
    <item>
      <trackback:ping>http://www.coeamyd.net/Trackback.aspx?guid=c915d182-ecff-4e37-b22d-70185058e97f</trackback:ping>
      <pingback:server>http://www.coeamyd.net/pingback.aspx</pingback:server>
      <pingback:target>http://www.coeamyd.net/PermaLink,guid,c915d182-ecff-4e37-b22d-70185058e97f.aspx</pingback:target>
      <dc:creator>Christoph Herold</dc:creator>
      <wfw:comment>http://www.coeamyd.net/CommentView,guid,c915d182-ecff-4e37-b22d-70185058e97f.aspx</wfw:comment>
      <wfw:commentRss>http://www.coeamyd.net/SyndicationService.asmx/GetEntryCommentsRss?guid=c915d182-ecff-4e37-b22d-70185058e97f</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Today, I was dumbfounded by LINQ's optimization skills. I built a query over a list
of items, which have an assigned category and a display order. I wanted to fetch a
list of the distinct categories, to display them as an overview, and have them sorted
according to the order of the entries themselves. My LINQ query looked something like
this:
</p>
        <pre>var categories = context.Entries.OrderBy(e =&gt; e.DisplayOrder).Select(e =&gt; e.Category).Distinct();</pre>
        <p>
Everything seemed to work, but when I started ordering things around, the categories
would remain as they were. What I also noticed, was that they were ordered alphabetically,
which perplexed me. I decided to look at the generated query, and lo and behold, there
was not a single ORDER BY clause in it. I took the generated query, added my own order
by clause to it, and got an error:
</p>
        <pre>ORDER BY items must appear in the select list if SELECT DISTINCT is specified.</pre>
        <p>
Of course: Distinct probably just sorts the values to make filtering out duplicates
easier. I left out the Distinct() method, and my ordering was the way I wanted to
have it.
</p>
        <p>
I have not yet worked on a sql-based solution for this, since my structure holding
the categories handled duplicate filtering by itself. And my data barely has any duplicates
in it anyways, so I left it at that.
</p>
        <p>
But what amazed me, was that LINQ actually left out the order by on its own accord,
since it would have caused an error. The LINQ to SQL parser really knows, what it's
doing, although in my case, I would have liked to get some sort of warning, that the
query will not behave as expected. Nonetheless, I love LINQ :-)
</p>
        <img width="0" height="0" src="http://www.coeamyd.net/aggbug.ashx?id=c915d182-ecff-4e37-b22d-70185058e97f" />
      </body>
      <title>OrderBy, Distinct, and LINQ's optimization skills</title>
      <guid isPermaLink="false">http://www.coeamyd.net/PermaLink,guid,c915d182-ecff-4e37-b22d-70185058e97f.aspx</guid>
      <link>http://www.coeamyd.net/PermaLink,guid,c915d182-ecff-4e37-b22d-70185058e97f.aspx</link>
      <pubDate>Thu, 16 Jul 2009 11:04:26 GMT</pubDate>
      <description>&lt;p&gt;
Today, I was dumbfounded by LINQ's optimization skills. I built a query over a list
of items, which have an assigned category and a display order. I wanted to fetch a
list of the distinct categories, to display them as an overview, and have them sorted
according to the order of the entries themselves. My LINQ query looked something like
this:
&lt;/p&gt;
&lt;pre&gt;var categories = context.Entries.OrderBy(e =&amp;gt; e.DisplayOrder).Select(e =&amp;gt; e.Category).Distinct();&lt;/pre&gt;
&lt;p&gt;
Everything seemed to work, but when I started ordering things around, the categories
would remain as they were. What I also noticed, was that they were ordered alphabetically,
which perplexed me. I decided to look at the generated query, and lo and behold, there
was not a single ORDER BY clause in it. I took the generated query, added my own order
by clause to it, and got an error:
&lt;/p&gt;
&lt;pre&gt;ORDER BY items must appear in the select list if SELECT DISTINCT is specified.&lt;/pre&gt;
&lt;p&gt;
Of course: Distinct probably just sorts the values to make filtering out duplicates
easier. I left out the Distinct() method, and my ordering was the way I wanted to
have it.
&lt;/p&gt;
&lt;p&gt;
I have not yet worked on a sql-based solution for this, since my structure holding
the categories handled duplicate filtering by itself. And my data barely has any duplicates
in it anyways, so I left it at that.
&lt;/p&gt;
&lt;p&gt;
But what amazed me, was that LINQ actually left out the order by on its own accord,
since it would have caused an error. The LINQ to SQL parser really knows, what it's
doing, although in my case, I would have liked to get some sort of warning, that the
query will not behave as expected. Nonetheless, I love LINQ :-)
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.coeamyd.net/aggbug.ashx?id=c915d182-ecff-4e37-b22d-70185058e97f" /&gt;</description>
      <comments>http://www.coeamyd.net/CommentView,guid,c915d182-ecff-4e37-b22d-70185058e97f.aspx</comments>
      <category>.NET;SQL Server;LINQ</category>
    </item>
  </channel>
</rss>