Best ways to format LINQ queries

.NetLinqFormattingCode Readability

.Net Problem Overview


Before you ignore / vote-to-close this question, I consider this a valid question to ask because code clarity is an important topic of discussion, it's essential to writing maintainable code and I would greatly appreciate answers from those who have come across this before.

I've recently run into this problem, LINQ queries can get pretty nasty real quick because of the large amount of nesting.

Below are some examples of the differences in formatting that I've come up with (for the same relatively non-complex query)

No Formatting

var allInventory = system.InventorySources.Select(src => new { Inventory = src.Value.GetInventory(product.OriginalProductId, true), Region = src.Value.Region }).GroupBy(i => i.Region, i => i.Inventory);

Elevated Formatting

var allInventory = system.InventorySources
	.Select(src => 
		new { 
			Inventory = src.Value.GetInventory(product.OriginalProductId, true), 
			Region = src.Value.Region })
				.GroupBy(
					i => i.Region, 
					i => i.Inventory);

Block Formatting

var allInventory = system.InventorySources
	.Select(
		src => new 
		{ 
			Inventory = src.Value.GetInventory(product.OriginalProductId, true), 
			Region = src.Value.Region 
		})
		.GroupBy(
			i => i.Region, 
			i => i.Inventory
		);

List Formatting

var allInventory = system.InventorySources
	.Select(src => new { Inventory = src.Value.GetInventory(product.OriginalProductId, true), Region = src.Value.Region })
	.GroupBy(i => i.Region, i => i.Inventory);

I want to come up with a standard for linq formatting so that it maximizes readability & understanding and looks clean and professional. So far I can't decide so I turn the question to the professionals here.

.Net Solutions


Solution 1 - .Net

My Formatting:

var allInventory = system.InventorySources
  .Select(src => new
  {
    Inventory = src.Value.GetInventory(product.OriginalProductId, true),
    Region = src.Value.Region
  })
  .GroupBy(
    i => i.Region,
    i => i.Inventory
  );

Notes:

  • Opening parens on methods are never worthy of a new line.
  • Closing parens match the indenting of the line that contains the opening paren.
  • The src => new stays on the same line as Select, because it's just not worthy of a new line.
  • The anonymous type always gets block treatment, just like if it was used outside of a query (but the closing paren is not worthy of a new line).
  • The two parameter GroupBy overload is not typically called. Although it could easy fit on a single line, use an extra line to make it clear that something unusual is happening.

Solution 2 - .Net

I have settled on Block formatting. It bothered my sense of "wasted space" for a while but ultimately everyone felt it was more readable by more people. As we were already putting braces on new lines it just fit better with the rest of the code. There is also less room for interpretation. We keep a cs file in the public store that has formatting examples...when somebody comes up with a unique hunk of linq we add it to the file...really helps the new guys.

Solution 3 - .Net

For me, it depends on the length of the query I have to make. For short simple statements like a basic select or simple joins I'll go withList Formatting because it makes it nice and easy to read without putting my code over loads of lines.

If I tend to have a rather complex or larger linq statement I go with block formatting so that it makes it easier for other people to read and follow what I was trying to do.

I don't think it's bad practice to have different formatting for different statements as long as you're consistant with how you approach it.

Solution 4 - .Net

Its very subjective.

I use the block formatting method.

I also check the code against Stylecop and make sure that it does not yield any stylecop warning.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionArenView Question on Stackoverflow
Solution 1 - .NetAmy BView Answer on Stackoverflow
Solution 2 - .NetRustyView Answer on Stackoverflow
Solution 3 - .NetRichard ReddyView Answer on Stackoverflow
Solution 4 - .NetPierre-Alain VigeantView Answer on Stackoverflow