<?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>Formatting LINQ Code</title><link>http://blogs.msdn.com/ericwhite/archive/2008/10/06/formatting-linq-code.aspx</link><description>[Blog Map] Over time, I’ve adjusted my code formatting style – changing the white space that I insert, or where I place the parentheses. In this post, I detail some aspects of my current code formatting approach, focusing only on formatting LINQ queries</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: Formatting LINQ Code</title><link>http://blogs.msdn.com/ericwhite/archive/2008/10/06/formatting-linq-code.aspx#8985699</link><pubDate>Tue, 07 Oct 2008 19:05:08 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8985699</guid><dc:creator>int19h</dc:creator><description>&lt;p&gt;I generally try to stick with the rules that VS code formatter uses (if only because I don't want my neat code to be broken by Ctrl+K, Ctrl+D). For chained method calls, when wrapping them on several lines, this means indenting the second line and those afterwards by a single tab (and not line up the dots).&lt;/p&gt;
&lt;p&gt;I always leave the opening parenthesis on the same line as the method it applies to in method calls, because this is consistent with method declarations. I.e., I do:&lt;/p&gt;
&lt;p&gt;.Select(&lt;/p&gt;
&lt;p&gt; &amp;nbsp;...);&lt;/p&gt;
&lt;p&gt;rather than:&lt;/p&gt;
&lt;p&gt;.Select(&lt;/p&gt;
&lt;p&gt; &amp;nbsp;...&lt;/p&gt;
&lt;p&gt;)&lt;/p&gt;
&lt;p&gt;because when I declare methods, I also do:&lt;/p&gt;
&lt;p&gt;void Foo(&lt;/p&gt;
&lt;p&gt; &amp;nbsp;int arg1,&lt;/p&gt;
&lt;p&gt; &amp;nbsp;int arg2);&lt;/p&gt;
&lt;p&gt;On the other hand, for curly braces, if they aren't both on the same line, I put each on its own separate line with nothing else there; so the lambda looks like this:&lt;/p&gt;
&lt;p&gt;item =&amp;gt;&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; ...&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;and not:&lt;/p&gt;
&lt;p&gt;item =&amp;gt; {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; ...&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;This one is actually sorta enforced by VS - if you write the second code snippet, you will see how VS will reformat it using the first style as you type the closing curly brace. It also does that for arrays and object initializers, by the way.&lt;/p&gt;
&lt;p&gt;On the whole, I find that if I use a raw method-call LINQ query, I try to fit it on a single line, and if that fails, I start to introduce temporary variables with descriptive names. Otherwise, I switch over to language-integrated query syntax which is much easier to arrange over several lines (and VS does a good job at doing it automatically, too) - but this is not an option for those long LINQ to XML queries, of course. &lt;/p&gt;
</description></item></channel></rss>