<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://programminglinq.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>Marco Russo</title><link>http://programminglinq.com/blogs/marcorusso/default.aspx</link><description /><dc:language>en</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.2)</generator><item><title>LINQ to SQL vs LINQ to Entities - decisions from ADO.NET team</title><link>http://programminglinq.com/blogs/marcorusso/archive/2008/11/03/linq-to-sql-vs-linq-to-entities-decisions-from-ado-net-team.aspx</link><pubDate>Sun, 02 Nov 2008 20:44:00 GMT</pubDate><guid isPermaLink="false">6f1fdd61-9c0b-497f-974b-3001d899dae0:165</guid><dc:creator>Marco.Russo</dc:creator><slash:comments>0</slash:comments><comments>http://programminglinq.com/blogs/marcorusso/comments/165.aspx</comments><wfw:commentRss>http://programminglinq.com/blogs/marcorusso/commentrss.aspx?PostID=165</wfw:commentRss><description>&lt;P&gt;To make a long story short: the ADO.NET team is now responsible of ADO.NET Entity Framework (including LINQ to Entities) and of LINQ to SQL (the last one was originally in charge of the a separated team, tied to the C# compiler).&lt;/P&gt;
&lt;P&gt;There is an evident overlapping between LINQ to SQL and LINQ to Entities and since the first day, Microsoft said that in the long run, these two solutions would have been merged into&amp;nbsp;a single one.&lt;BR&gt;Now, the roadmap that is arising is: Entity Framework &lt;A class="" href="http://blogs.msdn.com/adonet/archive/2008/10/31/clarifying-the-message-on-l2s-futures.aspx"&gt;will be improved&lt;/A&gt; adding features&amp;nbsp;that will be necessary to cover scenarios where LINQ to SQL today is preferred over LINQ to Entities and Entity Framework.&lt;/P&gt;
&lt;P&gt;There are a lot of comments - I suggest you &lt;A class="" href="http://oakleafblog.blogspot.com/2008/10/inq-and-entity-framework-posts-for.html#LINQtoSQL"&gt;starting here &lt;/A&gt;to get a good recap and pointers to many others.&lt;/P&gt;
&lt;P&gt;My personal opinion is that LINQ to SQL is very good in some scenarios and should not&amp;nbsp;be dropped until a good alternative (in EF?) is available. For example, I use LINQ to SQL to implement nightly processes that are part of ETL solutions. In these cases, I use LINQ to SQL to read data (expecially configuration data, but sometimes also source data) and use the SqlBulkCopy API to write data into destination tables. Having all the necessary into a single executable file, without external dependencies, is a big advantage for deployment (a single file to copy). Today LINQ to Entities would be slower, would have more files and would require .NET 3.5 SP1 on production servers (the last one would not be a real issue in my case). There are of course other scenarios when there is something that makes LINQ to SQL a better choice against the current version of Entity Framework.&lt;/P&gt;
&lt;P&gt;My hope is that a convergence of two partially overlapped frameworks is good, but at the same time this shouldn't be a penalization for the current users of the "losing" part. This will require several releases of .NET to be done, and I hope that in the meantime the LINQ to SQL engine will have a decent evolution to keep its current position of "light LINQ oriented DAL replacement to SQL Server".&lt;/P&gt;&lt;img src="http://programminglinq.com/aggbug.aspx?PostID=165" width="1" height="1"&gt;</description><category domain="http://programminglinq.com/blogs/marcorusso/archive/tags/LINQ/default.aspx">LINQ</category><category domain="http://programminglinq.com/blogs/marcorusso/archive/tags/Entity+Framework/default.aspx">Entity Framework</category><category domain="http://programminglinq.com/blogs/marcorusso/archive/tags/LINQ+to+SQL/default.aspx">LINQ to SQL</category></item><item><title>Book signing @ PDC 08</title><link>http://programminglinq.com/blogs/marcorusso/archive/2008/10/28/book-signing-pdc-08.aspx</link><pubDate>Mon, 27 Oct 2008 15:49:00 GMT</pubDate><guid isPermaLink="false">6f1fdd61-9c0b-497f-974b-3001d899dae0:164</guid><dc:creator>Marco.Russo</dc:creator><slash:comments>0</slash:comments><comments>http://programminglinq.com/blogs/marcorusso/comments/164.aspx</comments><wfw:commentRss>http://programminglinq.com/blogs/marcorusso/commentrss.aspx?PostID=164</wfw:commentRss><description>&lt;P&gt;If you are at PDC 2008, I'll be at book signing for &lt;A class="" href="http://www.amazon.com/gp/product/0735624003/?tag=se04-20"&gt;Programming Microsoft LINQ&lt;/A&gt; at bookstore on Tuesday 28, during the coffee break between 3:00 and 3:30 PM.&lt;BR&gt;I and Paolo will be happy to meet you and receive your direct feedback about our LINQ book.&lt;/P&gt;&lt;img src="http://programminglinq.com/aggbug.aspx?PostID=164" width="1" height="1"&gt;</description></item><item><title>Use IEnumerable as a source for SqlBulkCopy</title><link>http://programminglinq.com/blogs/marcorusso/archive/2008/09/17/use-ienumerable-as-a-source-for-sqlbulkcopy.aspx</link><pubDate>Wed, 17 Sep 2008 06:05:11 GMT</pubDate><guid isPermaLink="false">6f1fdd61-9c0b-497f-974b-3001d899dae0:162</guid><dc:creator>Marco.Russo</dc:creator><slash:comments>0</slash:comments><comments>http://programminglinq.com/blogs/marcorusso/comments/162.aspx</comments><wfw:commentRss>http://programminglinq.com/blogs/marcorusso/commentrss.aspx?PostID=162</wfw:commentRss><description>&lt;p&gt;Today I needed to use &lt;a href="http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlbulkcopy.aspx"&gt;SqlBulkCopy&lt;/a&gt; class passing an IEnumerable&amp;lt;T&amp;gt; as a source instead of a IDataReader. Before writing something that someone else could have already written, I made some search and I found &lt;a href="http://blogs.microsoft.co.il/blogs/aviwortzel/archive/2008/05/06/implementing-sqlbulkcopy-in-linq-to-sql.aspx"&gt;this interesting post&lt;/a&gt; that solve exactly this issue. In the post there is also a link to source code. Take also a look at performance optimization for getter as described in post comment.&lt;/p&gt;&lt;img src="http://programminglinq.com/aggbug.aspx?PostID=162" width="1" height="1"&gt;</description><category domain="http://programminglinq.com/blogs/marcorusso/archive/tags/LINQ/default.aspx">LINQ</category></item><item><title>Non-boolean LINQ predicates</title><link>http://programminglinq.com/blogs/marcorusso/archive/2008/09/15/non-boolean-linq-predicates.aspx</link><pubDate>Mon, 15 Sep 2008 09:18:23 GMT</pubDate><guid isPermaLink="false">6f1fdd61-9c0b-497f-974b-3001d899dae0:161</guid><dc:creator>Marco.Russo</dc:creator><slash:comments>0</slash:comments><comments>http://programminglinq.com/blogs/marcorusso/comments/161.aspx</comments><wfw:commentRss>http://programminglinq.com/blogs/marcorusso/commentrss.aspx?PostID=161</wfw:commentRss><description>&lt;p&gt;Bart De Smet just wrote a &lt;a href="http://community.bartdesmet.net/blogs/bart/archive/2008/09/14/who-ever-said-linq-predicates-need-to-be-boolean-valued.aspx"&gt;long post&lt;/a&gt; about LINQ predicates that can be defined without returning a boolean value.&lt;/p&gt; &lt;p&gt;This is something I partially evaluated writing the &lt;a href="http://www.amazon.com/gp/product/0735624003/?tag=se04-20"&gt;Programming Microsoft LINQ&lt;/a&gt; book, but in his post Bart goes very deep on this topic and shows a lot of interesting details and ideas.&lt;/p&gt;&lt;img src="http://programminglinq.com/aggbug.aspx?PostID=161" width="1" height="1"&gt;</description></item><item><title>Active queries LINQ</title><link>http://programminglinq.com/blogs/marcorusso/archive/2008/09/11/active-queries-linq.aspx</link><pubDate>Thu, 11 Sep 2008 08:12:51 GMT</pubDate><guid isPermaLink="false">6f1fdd61-9c0b-497f-974b-3001d899dae0:160</guid><dc:creator>Marco.Russo</dc:creator><slash:comments>0</slash:comments><comments>http://programminglinq.com/blogs/marcorusso/comments/160.aspx</comments><wfw:commentRss>http://programminglinq.com/blogs/marcorusso/commentrss.aspx?PostID=160</wfw:commentRss><description>&lt;p&gt;Paul Stovell made a presentation on "&lt;a href="http://www.paulstovell.com/blog/presentation-teched-australia-2008"&gt;Reactive Programming and Bindable LINQ&lt;/a&gt;" at TechED Australia 2008 (unfortunately, I was at the antipode in Italy, but the topic is really interesting). I didn't know there are projects somewhat similar to &lt;a href="http://www.codeplex.com/bindablelinq"&gt;Bindable LINQ&lt;/a&gt; in CodePlex: &lt;a href="http://www.codeplex.com/Obtics"&gt;Obtics&lt;/a&gt; and &lt;a href="http://www.codeplex.com/clinq"&gt;Continuous LINQ&lt;/a&gt;. I really like the idea of defining "live" queries with LINQ.&lt;/p&gt;&lt;img src="http://programminglinq.com/aggbug.aspx?PostID=160" width="1" height="1"&gt;</description><category domain="http://programminglinq.com/blogs/marcorusso/archive/tags/LINQ/default.aspx">LINQ</category></item><item><title>LINQ to SQL and the procedure cache of SQL Server</title><link>http://programminglinq.com/blogs/marcorusso/archive/2008/08/28/linq-to-sql-and-the-procedure-cache-of-sql-server.aspx</link><pubDate>Thu, 28 Aug 2008 00:14:42 GMT</pubDate><guid isPermaLink="false">6f1fdd61-9c0b-497f-974b-3001d899dae0:158</guid><dc:creator>Marco.Russo</dc:creator><slash:comments>1</slash:comments><comments>http://programminglinq.com/blogs/marcorusso/comments/158.aspx</comments><wfw:commentRss>http://programminglinq.com/blogs/marcorusso/commentrss.aspx?PostID=158</wfw:commentRss><description>&lt;p&gt;I just received a mail from &lt;a href="http://sqlblog.com/blogs/adam_machanic/"&gt;Adam Machanic&lt;/a&gt; that pointed me to &lt;a href="https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=363290"&gt;this bug&lt;/a&gt; (I would call it a performance issue) about the construction of SQL statements generated by the LINQ to SQL engine.&lt;/p&gt; &lt;p&gt;The issue: every string passed as a constant in the query will be auto-parameterized using the length of the passed string, even when you used a string variable into the LINQ query. If you write something like &lt;/p&gt; &lt;p&gt;&lt;font color="#0000ff" size="1"&gt;&lt;font color="#0000ff" size="1"&gt;&lt;font size="2"&gt;string&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; s = &lt;font color="#a31515"&gt;&lt;font color="#a31515"&gt;"Wine"&lt;/font&gt;&lt;/font&gt;;&lt;br&gt;&lt;/font&gt;&lt;font color="#0000ff"&gt;&lt;font color="#0000ff"&gt;&lt;font size="2"&gt;var&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; query =&lt;br&gt;&lt;/font&gt;&lt;font size="2"&gt;&lt;font color="#0000ff"&gt;&lt;font color="#0000ff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; from&lt;/font&gt;&lt;/font&gt; x &lt;font color="#0000ff"&gt;&lt;font color="#0000ff"&gt;in&lt;/font&gt;&lt;/font&gt; db.Products&lt;br&gt;&lt;/font&gt;&lt;font size="2"&gt;&lt;font color="#0000ff"&gt;&lt;font color="#0000ff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; where&lt;/font&gt;&lt;/font&gt; x.ProductName == s&lt;br&gt;&lt;font color="#0000ff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;&lt;font color="#0000ff"&gt;&lt;font color="#0000ff"&gt;select&lt;/font&gt;&lt;/font&gt; x;&lt;/font&gt;&lt;/p&gt; &lt;p&gt;you will see that a parameter of type NVARCHAR(4) will be passed to the generated SQL query. The next execution of query might contain a different value in the &lt;em&gt;s&lt;/em&gt; parameter, and for this reason a different parameter type might be used: if the length of the string in the &lt;em&gt;s &lt;/em&gt;variable changes, then the same query will be sent to SQL Server, but using a different type in the &lt;em&gt;sp_executesql &lt;/em&gt;parameters .For example, a NVARCHAR(5) would be used whether &lt;em&gt;s&lt;/em&gt; contains"Bread".&lt;/p&gt; &lt;p&gt;The consequence of this behavior is that you could have a non-optimal performance from SQL Server and, more important, the procedure cache could be filled up with several copies of the same query, differing each other only in the length of the parameter type.&lt;/p&gt; &lt;p&gt;I agree with Adam: this is something to be fixed. But my suspect is that we will get a "by design" answer another time...&lt;/p&gt;&lt;img src="http://programminglinq.com/aggbug.aspx?PostID=158" width="1" height="1"&gt;</description></item><item><title>IQueryable under the cover</title><link>http://programminglinq.com/blogs/marcorusso/archive/2008/08/17/iqueryable-under-the-cover.aspx</link><pubDate>Sun, 17 Aug 2008 09:07:48 GMT</pubDate><guid isPermaLink="false">6f1fdd61-9c0b-497f-974b-3001d899dae0:155</guid><dc:creator>Marco.Russo</dc:creator><slash:comments>0</slash:comments><comments>http://programminglinq.com/blogs/marcorusso/comments/155.aspx</comments><wfw:commentRss>http://programminglinq.com/blogs/marcorusso/commentrss.aspx?PostID=155</wfw:commentRss><description>&lt;p&gt;In the &lt;a href="http://www.amazon.com/gp/product/0735624003/?tag=se04-20"&gt;Programming Microsoft LINQ&lt;/a&gt; book we dedicated two whole chapters (76 pages) about the writing of a IQueryable LINQ provider: one is about expression trees and the other covers the several ways to extend LINQ, including the writing of an IQueryable provider. I know that the subject is complex and probably is not necessary to every programmer. However, a good understanding of what happens under the cover of an IQueryable provider is good for everyone using any flavor of LINQ: when you debug your code, it might help you in finding issues faster.&lt;/p&gt; &lt;p&gt;I wrote this introduction just to explain why you should read &lt;a href="http://community.bartdesmet.net/blogs/bart/archive/2008/08/15/the-most-funny-interface-of-the-year-iqueryable-lt-t-gt.aspx"&gt;this post&lt;/a&gt; of &lt;a href="http://community.bartdesmet.net/blogs/bart/about.aspx"&gt;Bart De Smet&lt;/a&gt;, which is undoubtedly shorter than the corresponding chapter of our book and gives you a very good step-by-step introduction of the inner workings of an IQueryable LINQ provider. Then, if you really like this kind of things, you have another good reason to read &lt;a href="http://programminglinq.com/"&gt;the book&lt;/a&gt; :-)&lt;/p&gt;&lt;img src="http://programminglinq.com/aggbug.aspx?PostID=155" width="1" height="1"&gt;</description></item><item><title>Important LINQ Changes in .NET 3.5 SP1</title><link>http://programminglinq.com/blogs/marcorusso/archive/2008/08/12/important-linq-changes-in-net-3-5-sp1.aspx</link><pubDate>Tue, 12 Aug 2008 07:55:25 GMT</pubDate><guid isPermaLink="false">6f1fdd61-9c0b-497f-974b-3001d899dae0:154</guid><dc:creator>Marco.Russo</dc:creator><slash:comments>0</slash:comments><comments>http://programminglinq.com/blogs/marcorusso/comments/154.aspx</comments><wfw:commentRss>http://programminglinq.com/blogs/marcorusso/commentrss.aspx?PostID=154</wfw:commentRss><description>&lt;p&gt;Dinesh Kulkarni wrote an &lt;a href="http://blogs.msdn.com/dinesh.kulkarni/archive/2008/08/10/net-fx-3-5-sp1-two-perf-improvements-linq-to-objects-and-linq-to-sql.aspx"&gt;important post about changes&lt;/a&gt; in LINQ introduced by &lt;a href="http://msdn.microsoft.com/en-us/vstudio/products/cc533447.aspx"&gt;.NET 3.5 SP1&lt;/a&gt; that has been released yesterday.&lt;/p&gt; &lt;p&gt;One of the interesting changes is in the Cast&amp;lt;T&amp;gt; operator and its behavior is better described in &lt;a href="http://blogs.msdn.com/ed_maurer/archive/2008/02/16/breaking-change-in-linq-queries-using-explicitly-typed-range-variables.aspx"&gt;this post&lt;/a&gt; by Ed Maurer. I think that the side effects of this change should be limited, because the use of explicit type for the range variable in a query expression (i.e. &lt;font color="#0000ff"&gt;from &lt;strong&gt;&lt;font color="#008000"&gt;int&lt;/font&gt;&lt;/strong&gt; n in numbers select...&lt;/font&gt; instead of &lt;font color="#0000ff"&gt;from n in numbers select...&lt;/font&gt;) is not very common. In fact, I don't remember examples of its usage in our &lt;a href="http://programminglinq.com/"&gt;Programming LINQ&lt;/a&gt; book. Take care of this change if you used (or will use) this syntax.&lt;/p&gt;&lt;img src="http://programminglinq.com/aggbug.aspx?PostID=154" width="1" height="1"&gt;</description></item><item><title>Dangerous use of ArrayList in Lambda Expressions</title><link>http://programminglinq.com/blogs/marcorusso/archive/2008/08/01/dangerous-use-of-arraylist-in-lambda-expressions.aspx</link><pubDate>Fri, 01 Aug 2008 10:53:07 GMT</pubDate><guid isPermaLink="false">6f1fdd61-9c0b-497f-974b-3001d899dae0:153</guid><dc:creator>Marco.Russo</dc:creator><slash:comments>0</slash:comments><comments>http://programminglinq.com/blogs/marcorusso/comments/153.aspx</comments><wfw:commentRss>http://programminglinq.com/blogs/marcorusso/commentrss.aspx?PostID=153</wfw:commentRss><description>&lt;p&gt;I have just validated &lt;a href="https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=359523"&gt;this bug&lt;/a&gt; posted on Connect. It seems a compiler issue, I'd like to read a Microsoft answer about this.&lt;/p&gt; &lt;p&gt;However, the general issue is that using ArrayList in a lambda expression with a collection initializer could be dangerous. There are not so many reasons to use an ArrayList in a lambda expression, unless you are refactoring or working with legacy code that cannot be modified upgrading ArrayList to generic collections.&lt;/p&gt;&lt;img src="http://programminglinq.com/aggbug.aspx?PostID=153" width="1" height="1"&gt;</description></item><item><title>The adoption of LINQ</title><link>http://programminglinq.com/blogs/marcorusso/archive/2008/07/25/the-adoption-of-linq.aspx</link><pubDate>Fri, 25 Jul 2008 07:06:38 GMT</pubDate><guid isPermaLink="false">6f1fdd61-9c0b-497f-974b-3001d899dae0:152</guid><dc:creator>Marco.Russo</dc:creator><slash:comments>0</slash:comments><comments>http://programminglinq.com/blogs/marcorusso/comments/152.aspx</comments><wfw:commentRss>http://programminglinq.com/blogs/marcorusso/commentrss.aspx?PostID=152</wfw:commentRss><description>&lt;p&gt;Eric White has written an interesting post titled &lt;a href="http://blogs.msdn.com/ericwhite/archive/2008/07/24/are-developers-using-linq.aspx"&gt;"Are developers using LINQ?"&lt;/a&gt; - there are interesting considerations about the adoption of functional programming too, but the most interesting part for me is the list of comment of the post. A lot of people described the adoption of LINQ into their team or company, and there is a spread variety of comments (good and bad).&lt;/p&gt; &lt;p&gt;An interesting comment is about the future adoption of F# when it will be shipped, because of the complete adoption of functional programming (C# 3 is not a complete functional programming like F# is). I suggest you to take a look at this post and its comments, because it gives you an idea of what is going on out there.&lt;/p&gt;&lt;img src="http://programminglinq.com/aggbug.aspx?PostID=152" width="1" height="1"&gt;</description></item><item><title>Multiple Results with LINQ to SQL</title><link>http://programminglinq.com/blogs/marcorusso/archive/2008/07/24/multiple-results-with-linq-to-sql.aspx</link><pubDate>Thu, 24 Jul 2008 06:49:59 GMT</pubDate><guid isPermaLink="false">6f1fdd61-9c0b-497f-974b-3001d899dae0:150</guid><dc:creator>Marco.Russo</dc:creator><slash:comments>0</slash:comments><comments>http://programminglinq.com/blogs/marcorusso/comments/150.aspx</comments><wfw:commentRss>http://programminglinq.com/blogs/marcorusso/commentrss.aspx?PostID=150</wfw:commentRss><description>&lt;p&gt;I just read &lt;a href="http://tonesdotnetblog.wordpress.com/2008/07/23/linq-to-sql-batches-and-multiple-results-without-stored-procedures-by-tony-wright/"&gt;a post&lt;/a&gt; about getting multiple results with LINQ to SQL without using stored procedures. This technique is interesting when you have multiple queries returning a few rows each one and you want to save time by skipping some roundtrip between your program and SQL Server. Looking at the post, I immediately thought that it would be interesting comparing this solution with an asynchronous one, executing each query in a different thread. I don't have time to make some benchmark, but it would be interesting to make a comparison between these two techniques.&lt;/p&gt;&lt;img src="http://programminglinq.com/aggbug.aspx?PostID=150" width="1" height="1"&gt;</description></item><item><title>Use of Distinct and OrderBy in LINQ</title><link>http://programminglinq.com/blogs/marcorusso/archive/2008/07/20/use-of-distinct-and-orderby-in-linq.aspx</link><pubDate>Sun, 20 Jul 2008 08:16:26 GMT</pubDate><guid isPermaLink="false">6f1fdd61-9c0b-497f-974b-3001d899dae0:145</guid><dc:creator>Marco.Russo</dc:creator><slash:comments>1</slash:comments><comments>http://programminglinq.com/blogs/marcorusso/comments/145.aspx</comments><wfw:commentRss>http://programminglinq.com/blogs/marcorusso/commentrss.aspx?PostID=145</wfw:commentRss><description>&lt;p&gt;A few days ago I found a bug in a program written using LINQ to SQL, which was caused by years of use of SQL. The requirement was something like: get the distinct values of (bla bla bla) sorted alphabetically. An example of the required query with Northwind would be the following one:&lt;/p&gt; &lt;p&gt;&lt;pre&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;DISTINCT
&lt;/span&gt;&lt;span style="color:#000000;"&gt;        e&lt;/span&gt;&lt;span style="color:#808080;"&gt;.&lt;/span&gt;&lt;span style="color:#000000;"&gt;LastName
&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt;&lt;span style="color:#000000;"&gt;    Orders o
&lt;/span&gt;&lt;span style="color:#808080;"&gt;LEFT&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;JOIN&lt;/span&gt;&lt;span style="color:#000000;"&gt; [Employees] e
        &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;ON&lt;/span&gt;&lt;span style="color:#000000;"&gt; e&lt;/span&gt;&lt;span style="color:#808080;"&gt;.&lt;/span&gt;&lt;span style="color:#000000;"&gt;[EmployeeID] &lt;/span&gt;&lt;span style="color:#808080;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; o&lt;/span&gt;&lt;span style="color:#808080;"&gt;.&lt;/span&gt;&lt;span style="color:#000000;"&gt;[EmployeeID]
&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;ORDER&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;BY&lt;/span&gt;&lt;span style="color:#000000;"&gt; e&lt;/span&gt;&lt;span style="color:#808080;"&gt;.&lt;/span&gt;&lt;span style="color:#000000;"&gt;LastName &lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Fundamentally, we are using both a DISTINCT and an ORDER BY statement in SQL.
&lt;p&gt;Now, if you create a NorthwindDataContext importing the Order and Employee tables, you can try to write a similar statement in LINQ to SQL. Unfortunately, the Distinct clause is not part of the query syntax and the most intuitive path could be the one of calling Distinct at the end of your statement, like in the following query:
&lt;p&gt;&lt;pre&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; queryA =
    (&lt;/span&gt;&lt;span style="color:#000000;"&gt;from&lt;/span&gt;&lt;span style="color:#000000;"&gt; o &lt;/span&gt;&lt;span style="color:#000000;"&gt;in&lt;/span&gt;&lt;span style="color:#000000;"&gt; db.Orders
     &lt;/span&gt;&lt;span style="color:#000000;"&gt;orderby&lt;/span&gt;&lt;span style="color:#000000;"&gt; o.Employee.LastName
     &lt;/span&gt;&lt;span style="color:#000000;"&gt;select&lt;/span&gt;&lt;span style="color:#000000;"&gt; o.Employee.LastName)
     .Distinct();&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;However, the Distinct clause is removing the sort condition defined by the &lt;em&gt;orderby&lt;/em&gt; keyword. In fact, the SQL statement sent to the database is the following one:
&lt;p&gt;&lt;pre&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;DISTINCT
&lt;/span&gt;&lt;span style="color:#000000;"&gt;        [t1]&lt;/span&gt;&lt;span style="color:#808080;"&gt;.&lt;/span&gt;&lt;span style="color:#000000;"&gt;[LastName]
&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt;&lt;span style="color:#000000;"&gt;    [dbo]&lt;/span&gt;&lt;span style="color:#808080;"&gt;.&lt;/span&gt;&lt;span style="color:#000000;"&gt;[Orders] &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt;&lt;span style="color:#000000;"&gt; [t0]
&lt;/span&gt;&lt;span style="color:#808080;"&gt;LEFT&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;OUTER&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;JOIN&lt;/span&gt;&lt;span style="color:#000000;"&gt; [dbo]&lt;/span&gt;&lt;span style="color:#808080;"&gt;.&lt;/span&gt;&lt;span style="color:#000000;"&gt;[Employees] &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt;&lt;span style="color:#000000;"&gt; [t1]
        &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;ON&lt;/span&gt;&lt;span style="color:#000000;"&gt; [t1]&lt;/span&gt;&lt;span style="color:#808080;"&gt;.&lt;/span&gt;&lt;span style="color:#000000;"&gt;[EmployeeID] &lt;/span&gt;&lt;span style="color:#808080;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; [t0]&lt;/span&gt;&lt;span style="color:#808080;"&gt;.&lt;/span&gt;&lt;span style="color:#000000;"&gt;[EmployeeID]&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;This behavior might appear strange. The problem is that the Distinct operator does not grant that it will maintain the original order of values. Applied to LINQ to SQL, this mean that a sort constraint can be ignored in the case of a query like &lt;em&gt;queryA&lt;/em&gt;.
&lt;p&gt;The solution is pretty sample: put the &lt;em&gt;OrderBy &lt;/em&gt;operator &lt;u&gt;after &lt;/u&gt;the &lt;em&gt;Distinct &lt;/em&gt;one, like in the following &lt;em&gt;queryB&lt;/em&gt; definition:
&lt;p&gt;&lt;pre&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; queryB = 
    (&lt;/span&gt;&lt;span style="color:#000000;"&gt;from&lt;/span&gt;&lt;span style="color:#000000;"&gt; o &lt;/span&gt;&lt;span style="color:#000000;"&gt;in&lt;/span&gt;&lt;span style="color:#000000;"&gt; db.Orders
     &lt;/span&gt;&lt;span style="color:#000000;"&gt;select&lt;/span&gt;&lt;span style="color:#000000;"&gt; o.Employee.LastName)
    .Distinct().OrderBy( n =&amp;gt; n );
&lt;p&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;&lt;/pre&gt;
&lt;p&gt;&lt;span style="color:#000000;"&gt;&lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;/span&gt;This will result in the following SQL statement sent to Northwind:
&lt;p&gt;&lt;pre&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt;&lt;span style="color:#000000;"&gt;  [t2]&lt;/span&gt;&lt;span style="color:#808080;"&gt;.&lt;/span&gt;&lt;span style="color:#000000;"&gt;[LastName]
&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt;&lt;span style="color:#000000;"&gt;    &lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;DISTINCT
&lt;/span&gt;&lt;span style="color:#000000;"&gt;                    [t1]&lt;/span&gt;&lt;span style="color:#808080;"&gt;.&lt;/span&gt;&lt;span style="color:#000000;"&gt;[LastName]
          &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt;&lt;span style="color:#000000;"&gt;      [dbo]&lt;/span&gt;&lt;span style="color:#808080;"&gt;.&lt;/span&gt;&lt;span style="color:#000000;"&gt;[Orders] &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt;&lt;span style="color:#000000;"&gt; [t0]
          &lt;/span&gt;&lt;span style="color:#808080;"&gt;LEFT&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;OUTER&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;JOIN&lt;/span&gt;&lt;span style="color:#000000;"&gt; [dbo]&lt;/span&gt;&lt;span style="color:#808080;"&gt;.&lt;/span&gt;&lt;span style="color:#000000;"&gt;[Employees] &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt;&lt;span style="color:#000000;"&gt; [t1]
                    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;ON&lt;/span&gt;&lt;span style="color:#000000;"&gt; [t1]&lt;/span&gt;&lt;span style="color:#808080;"&gt;.&lt;/span&gt;&lt;span style="color:#000000;"&gt;[EmployeeID] &lt;/span&gt;&lt;span style="color:#808080;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; [t0]&lt;/span&gt;&lt;span style="color:#808080;"&gt;.&lt;/span&gt;&lt;span style="color:#000000;"&gt;[EmployeeID]
        &lt;/span&gt;&lt;span style="color:#808080;"&gt;)&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt;&lt;span style="color:#000000;"&gt; [t2]
&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;ORDER&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;BY&lt;/span&gt;&lt;span style="color:#000000;"&gt; [t2]&lt;/span&gt;&lt;span style="color:#808080;"&gt;.&lt;/span&gt;&lt;span style="color:#000000;"&gt;[LastName]&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;If you remove some syntax redundancy, this is exactly the same query I wrote at the beginning of my post.
&lt;p&gt;The lesson is: in a SQL query, the position of an operator is not relevant until operators belong to the same SELECT/FROM statement. In LINQ, this is not true and the conversion to SQL could remove LINQ operators when their operation might be ignored by other operators in the same LINQ query.
&lt;p&gt;Final consideration: initially I considered that the compiler could emit some warning in case a query reduction is done like in the queryA case. Unfortunately, the query reduction operation is done by the LINQ to SQL provider at execution time and not during compilation. A warning could still be possible, but it's something that I would move to tools like FxCop.
&lt;p&gt;&lt;pre&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;/pre&gt;&lt;/p&gt;&lt;/span&gt;&lt;img src="http://programminglinq.com/aggbug.aspx?PostID=145" width="1" height="1"&gt;</description></item><item><title>LINQ query optimizations</title><link>http://programminglinq.com/blogs/marcorusso/archive/2008/07/19/linq-query-optimizations.aspx</link><pubDate>Fri, 18 Jul 2008 20:28:00 GMT</pubDate><guid isPermaLink="false">6f1fdd61-9c0b-497f-974b-3001d899dae0:144</guid><dc:creator>Marco.Russo</dc:creator><slash:comments>1</slash:comments><comments>http://programminglinq.com/blogs/marcorusso/comments/144.aspx</comments><wfw:commentRss>http://programminglinq.com/blogs/marcorusso/commentrss.aspx?PostID=144</wfw:commentRss><description>&lt;P&gt;Look at &lt;A class="" href="http://odetocode.com/Blogs/scott/archive/2008/07/14/12192.aspx"&gt;this excellent blog post&lt;/A&gt; written by &lt;A class="" href="http://odetocode.com/Blogs/scott"&gt;K. Scott Allen&lt;/A&gt;. I completely agree with him: don't try to optimize a LINQ query until you measure its performance and understand it is really a bottleneck that needs to be improved.&lt;/P&gt;
&lt;P&gt;An interesting consideration I never made before is that you can call the OrderBy extension method &lt;EM&gt;after&lt;/EM&gt; the Select and not before. Yes, using the query syntax of C# you are used to put the Select after the OrderBy, but &lt;EM&gt;sometime&lt;/EM&gt; it could be better to invert this order (the reasons are already well explained in the Scott Allen's post).&lt;/P&gt;
&lt;P&gt;And, of course, consider that performance have to be evaluated in two dimension: time &lt;EM&gt;and &lt;/EM&gt;space. And, sooner than later, a third dimension (parallelism) will gain the same importance.&lt;/P&gt;&lt;img src="http://programminglinq.com/aggbug.aspx?PostID=144" width="1" height="1"&gt;</description></item><item><title>Implement progress reporting and cancellation of LINQ queries</title><link>http://programminglinq.com/blogs/marcorusso/archive/2008/07/18/implement-progress-reporting-and-cancellation-of-linq-queries.aspx</link><pubDate>Fri, 18 Jul 2008 01:06:29 GMT</pubDate><guid isPermaLink="false">6f1fdd61-9c0b-497f-974b-3001d899dae0:141</guid><dc:creator>Marco.Russo</dc:creator><slash:comments>1</slash:comments><comments>http://programminglinq.com/blogs/marcorusso/comments/141.aspx</comments><wfw:commentRss>http://programminglinq.com/blogs/marcorusso/commentrss.aspx?PostID=141</wfw:commentRss><description>&lt;p&gt;Samuel Jack wrote two interesting posts discussing possible extension methods for LINQ. One is to implement &lt;a href="http://blog.functionalfun.net/2008/07/reporting-progress-during-linq-queries.html"&gt;progress reporting&lt;/a&gt; of a LINQ query. The other is to implement a way to &lt;a href="http://blog.functionalfun.net/2008/07/cancelling-long-running-linq-queries.html"&gt;cancel a running LINQ query&lt;/a&gt;.&lt;/p&gt; &lt;p&gt;Both implementations are very simple and they are very good to illustrate how LINQ can be extended and manipulated in a simple way by using extension methods.&lt;/p&gt;&lt;img src="http://programminglinq.com/aggbug.aspx?PostID=141" width="1" height="1"&gt;</description><category domain="http://programminglinq.com/blogs/marcorusso/archive/tags/LINQ/default.aspx">LINQ</category></item><item><title>LINQ to SQL and varchar(1) fields</title><link>http://programminglinq.com/blogs/marcorusso/archive/2008/07/17/linq-to-sql-and-varchar-1-fields.aspx</link><pubDate>Thu, 17 Jul 2008 07:03:49 GMT</pubDate><guid isPermaLink="false">6f1fdd61-9c0b-497f-974b-3001d899dae0:143</guid><dc:creator>Marco.Russo</dc:creator><slash:comments>1</slash:comments><comments>http://programminglinq.com/blogs/marcorusso/comments/143.aspx</comments><wfw:commentRss>http://programminglinq.com/blogs/marcorusso/commentrss.aspx?PostID=143</wfw:commentRss><description>&lt;p&gt;If you are using the Object Relational Designer of LINQ to SQL creating an entity of an existing table that has some VARCHAR(1) fields, you are going into this issue.&lt;/p&gt; &lt;p&gt;The data member created in C# is char instead of string. If the field is always filled with one char, this works. But if you try to read a row from the table containing an empty string (not a NULL field, but a field of zero characters) you will get this exception:&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;&lt;em&gt;String must be exactly one character long.&lt;/em&gt;&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;This behavior has been already described in &lt;a href="http://rjdudley.com/blog/LINQErrorStringMustBeExactlyOneCharacterLong.aspx"&gt;this post&lt;/a&gt; and in the &lt;a href="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2291060&amp;amp;SiteID=1"&gt;LINQ forum&lt;/a&gt;. But one more warning could be important: this is a latent error that will express yourself only at runtime if you don't fix. Thus, be careful whenever you have some VARCHAR(1) fields in your tables.&lt;/p&gt;&lt;img src="http://programminglinq.com/aggbug.aspx?PostID=143" width="1" height="1"&gt;</description></item></channel></rss>