<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.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>Trivial Projections Are (Usually) Optimized Away</title><link>http://blogs.msdn.com/ericlippert/archive/2008/05/12/trivial-projections-are-usually-optimized-away.aspx</link><description>OK, computers aren't entirely dumb when it comes to LINQ. Here's an example of a place where we're a bit smarter. Consider the following query: IEnumerable&amp;lt;int&amp;gt; query = from n in number_array orderby n select n; Does this get transformed by the</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: Trivial Projections Are (Usually) Optimized Away</title><link>http://blogs.msdn.com/ericlippert/archive/2008/05/12/trivial-projections-are-usually-optimized-away.aspx#8496040</link><pubDate>Mon, 12 May 2008 21:53:02 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8496040</guid><dc:creator>jonskeet</dc:creator><description>&lt;p&gt;I think the spec is actually reasonably clear on what happens - although as I've argued before, it makes it even more sensible to be able to omit the select clause - but I'm still not quite as clear in my mind about why the identity relationship is such a bad thing.&lt;/p&gt;
&lt;p&gt;I suspect it's because I haven't seen any examples of where it would make a difference. Do you have any compelling examples where it would be really obviously a bad thing to do? I think I understand it from a &amp;quot;philosophical&amp;quot; point of view, but I can't immediately see an urgent practical problem.&lt;/p&gt;
</description></item><item><title>re: Trivial Projections Are (Usually) Optimized Away</title><link>http://blogs.msdn.com/ericlippert/archive/2008/05/12/trivial-projections-are-usually-optimized-away.aspx#8496431</link><pubDate>Mon, 12 May 2008 22:49:23 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8496431</guid><dc:creator>Eric Lippert</dc:creator><description>&lt;p&gt;Consider the code I blogged about here:&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://blogs.msdn.com/ericlippert/archive/2008/03/28/why-can-t-i-access-a-protected-member-from-a-derived-class-part-two-why-can-i.aspx"&gt;http://blogs.msdn.com/ericlippert/archive/2008/03/28/why-can-t-i-access-a-protected-member-from-a-derived-class-part-two-why-can-i.aspx&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The code requires that the private hashset only be accessed via the read-only IEnumerable&amp;lt;T&amp;gt; enumerator. If external code can get at the underlying hash set via a cast, then it can muck up the carefully maintained invariants of the class. That's why even though the collection implements IEnumerable, I had the code explicitly implement an iterator, to protect the mutable collection from undesired mutation.&lt;/p&gt;
&lt;p&gt;You could imagine implementing the iterator as a degenerate query rather than an iterator block; we would want the result to be the same. The degenerate query should protect the mutable collection from mutation.&lt;/p&gt;
</description></item><item><title>re: Trivial Projections Are (Usually) Optimized Away</title><link>http://blogs.msdn.com/ericlippert/archive/2008/05/12/trivial-projections-are-usually-optimized-away.aspx#8496976</link><pubDate>Tue, 13 May 2008 00:25:29 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8496976</guid><dc:creator>jonskeet</dc:creator><description>&lt;p&gt;Right, I suppose the important part is when you're returning the result of a query to another piece of code. That's quite possibly what I'd been missing before. Thanks for bearing with me :)&lt;/p&gt;
&lt;p&gt;Obviously the code which is *doing* the query to start with already has the original reference - I hadn't been widening my mental scope far enough.&lt;/p&gt;
&lt;p&gt;Just as a though, it would be nice to have an extension method on IEnumerable which explicitly does a &amp;quot;no-op select&amp;quot; to avoid having to write the yield return loop you had in the linked post. Easy to do, obviously, but it would be nice to have it in the framework. Maybe not for 3.5 SP1 though ;)&lt;/p&gt;
&lt;p&gt;Jon&lt;/p&gt;
</description></item><item><title>re: Trivial Projections Are (Usually) Optimized Away</title><link>http://blogs.msdn.com/ericlippert/archive/2008/05/12/trivial-projections-are-usually-optimized-away.aspx#8503090</link><pubDate>Wed, 14 May 2008 15:38:18 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8503090</guid><dc:creator>DRBlaise</dc:creator><description>&lt;p&gt;Eric,&lt;/p&gt;
&lt;p&gt;I just wanted to let you know how much I have enjoyed your last several posts. &amp;nbsp;Every one of your posts seems to have little seed of useful insight!&lt;/p&gt;
</description></item><item><title>re: Trivial Projections Are (Usually) Optimized Away</title><link>http://blogs.msdn.com/ericlippert/archive/2008/05/12/trivial-projections-are-usually-optimized-away.aspx#8503133</link><pubDate>Wed, 14 May 2008 16:23:43 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8503133</guid><dc:creator>Eric Lippert</dc:creator><description>&lt;p&gt;Thanks! &amp;nbsp;&lt;/p&gt;
</description></item><item><title>re: Trivial Projections Are (Usually) Optimized Away</title><link>http://blogs.msdn.com/ericlippert/archive/2008/05/12/trivial-projections-are-usually-optimized-away.aspx#8503935</link><pubDate>Wed, 14 May 2008 21:23:43 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8503935</guid><dc:creator>Bahador</dc:creator><description>&lt;p&gt;Ummm. I didn't know about that (and I my mind crashed on those seemingly contradictory statements from spec :D).&lt;/p&gt;
&lt;p&gt;Thanks!&lt;/p&gt;
&lt;p&gt;3.0 is the first version of C# that I haven't read its spec fully... yet!&lt;/p&gt;
</description></item><item><title>VCS Team Links for May 22, 2008</title><link>http://blogs.msdn.com/ericlippert/archive/2008/05/12/trivial-projections-are-usually-optimized-away.aspx#8533330</link><pubDate>Thu, 22 May 2008 22:12:32 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8533330</guid><dc:creator>Charlie Calvert's Community Blog</dc:creator><description>&lt;p&gt;Rather than place the links to the most recent C# team content directly in Community Convergence , I&lt;/p&gt;
</description></item><item><title>re: Trivial Projections Are (Usually) Optimized Away</title><link>http://blogs.msdn.com/ericlippert/archive/2008/05/12/trivial-projections-are-usually-optimized-away.aspx#9905705</link><pubDate>Sat, 10 Oct 2009 07:53:41 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9905705</guid><dc:creator>David Schmitt</dc:creator><description>&lt;p&gt;&amp;quot;This is important because the collection might be a mutable collection. One does not expect that the result of a query over that collection could be used to mutate the collection! The query results should be read-only.&amp;quot;&lt;/p&gt;
&lt;p&gt;Another argument why it's a shame that .net doesn't support immutable datastructures out of the box. LINQ to objects would have much more latitude in optimizing queries against such things.&lt;/p&gt;
</description></item></channel></rss>