2

Consider the following code formatting, taken from this answer.

var query = grades.GroupBy(student => student.Name)
                  .Select(group => 
                        new { Name = group.Key,
                              Students = group.OrderByDescending(x => x.Grade) })
                  .OrderBy(group => group.Students.First().Grade);

Note how the calls to GroupBy, Select and OrderBy all start exactly at the same position / are directly aligned below each other. How can I automatically format my code like this with either Visual Studio directly or using Resharper?

stefan.at.kotlin
  • 15,347
  • 38
  • 147
  • 270

1 Answers1

0

In ReSharper, under Code Editing -> C# -> Formatting Style -> Tabs, Indents, Alignment -> Align multiline constructs, check the Chained method calls box. It takes a bit of time but your really ought to have a thorough look through that Options dialogue.

enter image description here

John
  • 3,057
  • 1
  • 4
  • 10
  • ah great, thank you. I mixed this up with the Line breaks - Wrap chained method calls option. And using the full options instead of Configure code style from the context menu provides the menu shown in your screenshot. didn't know that yet, helps a lot. – stefan.at.kotlin Aug 07 '22 at 13:48
  • @John Is there an option to set it also in Visual Studio directly without ReSharper? – Cheshire Cat Feb 16 '23 at 15:40