开发者

Dealing with VB.NET Linq formatting in Visual Studio?

开发者 https://www.devze.com 2023-01-08 23:11 出处:网络
Does anyone else wrestle with getting VB.NET Linq code formatted the way they want it in Visual Studio?What are some tricks you have for how do you deal with it?I know that Visual Studio lets you unch

Does anyone else wrestle with getting VB.NET Linq code formatted the way they want it in Visual Studio? What are some tricks you have for how do you deal with it? I know that Visual Studio lets you uncheck the option for "Pretty listing (reformatting) of code", but most of the time it's a really handy setting to keep on. And with a team of developers who could have other settings, that's not really a viable solution. So my questions are:

  1. How have you dealt with formatting multiline Linq statements in VB without fighting with Visual Studio's code formatting?
  2. Is there a particular style guide for Linq in VB.NET that is recommended? Something that is readable, and that Visual Studio likes without the reformatting nightmares?

Edit -

My original question may not have had the clarity I desired. Here's a more concrete example. Take this fake linq query:

Dim query = From a In dc.GetTable(Of DAL.GenericTableDao)()
            Select New With {
              .ID = a.ID,
              .SomethingElse = a.SomethingElse,
              .MyOtherRecord = (
  开发者_运维知识库                From dtl In dc.GetTable(Of DAL.DetailTableDao)()
                  Where dtl.ID2 = a.ID _
                  AndAlso dtl.Code = a.Code _
                  Select dtl.RecordName
              ).FirstOrDefault(),
              .LastField = a.LastField
            }

Everything is all nicely formatted and lined up, but any changes to the query result in Visual Studio borking the formatting and only an immediate CTRL-Z will stop it. Which is fine if there's a standard way that VS would like to see multiline Linq formatted, but if not, then the constant reformatting VS tries makes no sense. I'm trying to see if I missed some standard or how others in the community deal with this effectively. (BTW - using tabs instead of spaces for indents. Not sure if that plays into this problem or not.)


Edit 2 -

Solved it. See answer below.


I solved it. I have Smart indenting turned on for VB. Tabs are fine, pretty listing of code is fine, but indents have to be set to Block in order for Visual Studio to leave well enough alone. Thanks to @rockinthesixstring and @rossisdead for rubber-ducking me through it.


I don't have a definitive guide, but I definitely have a preference. I prefer to put a break after everything.

Here's a lambda expression I use in my service layer

        Return _RegionRepository.GetRegions() _
            .Where(Function(r) (r.Region = region _
                                And r.ParentID = parentid _
                                And r.isActive)) _
            .FirstOrDefault()

Notice the underscore after every query item

Here's an update method that I use

        Dim _user = (From u In dc.Users
            Where u.ID = user.ID
            Select u).Single

        With _user
            .About = user.About
            .BirthDate = user.BirthDate
            .Email = user.Email
            .isClosed = user.isClosed
            .isProfileComplete = user.isProfileComplete
            .RegionID = user.RegionID
            .ParentRegionID = user.ParentRegionID
            .Reputation = user.Reputation
            .UserName = user.UserName
            .WebSite = user.WebSite
        End With

        dc.SubmitChanges()

Basically I like everything nice and clean and to have every "thought" on it's own line.

0

精彩评论

暂无评论...
验证码 换一张
取 消