Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq Query Formatting in Visual Studio

After typing the following into Visual Studio 2010,

using(var myEntities = new MyEntities())
{
    IQueryable<Employee> employees =
        from    e in myEntities.Employees
        where   e.Name == name &&
                e.Password == hasher.ComputeHash(password)
        select  e;

    ...  Code left out for simplicity ...

I encounter a formatting problem when I enter the closing brace. Specifically, the text editor reformats the spaces in my linq query so that I end up with,

using(var myEntities = new MyEntities())
{
    IQueryable<Employee> employees =
        from e in myEntities.Employees
        where e.Name == name &&
                e.Password == hasher.ComputeHash(password)
        select e;

    ...  Code left out for simplicity ...
}

Is it possible to change something in the formattings settings for Visual Studio to prevent the spaces in my linq query from being automatically deleted? I've tried Googling this, and I've looked through the Tools -> Options window of Visual Studio but wasn't able to find anything. Hopefully, I just overlooked something...

Many thanks in advance!

like image 232
Andrew Avatar asked Oct 10 '22 01:10

Andrew


1 Answers

In VS, go to Tools->Options, then Text Editor->C#->Formatting->General. Uncheck at least the first two checkboxes on this page. That will prevent VS from automatically formatting your code. However, there is no way to disable autoformat only for LINQ queries.

like image 102
Brent M. Spell Avatar answered Oct 17 '22 02:10

Brent M. Spell