Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best ways to format LINQ queries

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.

like image 346
Aren Avatar asked May 27 '10 20:05

Aren


People also ask

How many ways can you write LINQ queries?

LINQ provides you three different ways to write a LINQ query in C# or VB.

Which is faster LINQ or SQL?

More importantly: when it comes to querying databases, LINQ is in most cases a significantly more productive querying language than SQL. Compared to SQL, LINQ is simpler, tidier, and higher-level.

Is LINQ good for performance?

LINQ syntax is typically less efficient than a foreach loop. It's good to be aware of any performance tradeoff that might occur when you use LINQ to improve the readability of your code. And if you'd like to measure the performance difference, you can use a tool like BenchmarkDotNet to do so.


1 Answers

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.
like image 144
Amy B Avatar answered Oct 13 '22 21:10

Amy B