How do I set formatting rules for each clause in linq query syntax to appear on a new line

Hi,

I use StyleCop, which includes a readability rule (http://stylecop.soyuz5.com/SA1103.html) stating that all clauses in a linq query must appear on the same line, or on a new line.

I run into problems when I use a grouping in query syntax.

var result =
                    (from c in collection
                     where c.SomeProperty == true
                     group c by c.SomeOtherProperty
                     into grp select grp.OrderByDescending(c => c.AnotherProperty).First()).ToList();


I cannot find a way to make R# put the "into" and "select" clauses on a seperate line.

6 comments
Comment actions Permalink

Hello Tim,

There's a corresponding option in C# Formatter Settings. I've attached an image for a reference.

Thanks.



Attachment(s):
screen424.png
0
Comment actions Permalink

Hi Alex,

This option does not make a difference for me. The actual linq query is quite long with multiple from and where conditions, so whether the option you provided is ticked or not, the formatting comes out the same.

The issue seems to be specifcally related to using an "into" clause in a linq query. After the into, multiple other clauses are then placed on the same line.

Here is another example. I cannot get the line with "into" the format correctly:

            foreach (var rateEntry in from originCode in originCodes

                                      from destinationCode in destinationCodes

                                      from transportModes in transportModeGroups

                                      from containerType in containerTypes

                                      from serviceProvider in serviceProviders

                                      from commodity in commodities

                                      select (from re in rateEntries

                                              where

                                                  re.OriginCode == originCode &&

                                                  (re.DestinationCode == destinationCode ||

                                                   (string.IsNullOrEmpty(re.DestinationCode) && string.IsNullOrEmpty(destinationCode))) &&

                                                  transportModes.Contains(re.Mode) &&

                                                  (re.ContainerType == containerType || (string.IsNullOrEmpty(re.ContainerType) && string.IsNullOrEmpty(containerType))) &&

                                                  (re.ServiceProviderDatabaseKey == serviceProvider || (re.ServiceProviderDatabaseKey == null && serviceProvider == null)) &&

                                                  (re.CommodityCode == commodity || (string.IsNullOrEmpty(re.CommodityCode) && string.IsNullOrEmpty(commodity)))

                                              select re).SingleOrDefault()

                                      into rateEntry where rateEntry != null select rateEntry)

                rateLines.AddRange(rateEntry.RateLines.Where(rl => rateLines.All(rl2 => rl2.ChargeCode != rl.ChargeCode)));
0
Comment actions Permalink

Tim,

Our forum engine trimmed the code and line breaks.
Could you please attach a *.cs file with the actuall formatting with example how the code is formatted by R# and how'd you like it to be formatted? This will be much appreciated.

Thanks in advance!

0
Comment actions Permalink

Hi Alex,

It looks fine on my browser?

I would like to know how to format this following query:

from c in collection
where c.SomeProperty == true
group c by c.SomeOtherProperty
into grp select grp


To:

from c in collection
where c.SomeProperty == true
group c by c.SomeOtherProperty
into grp

select grp

Or maybe to:

from c in collection
where c.SomeProperty == true
group c by c.SomeOtherProperty into grp

select grp

0
Comment actions Permalink

Hi Tim,

I've checked that out, and it looks like there's no such option in ReSharper Formatter for now.

We apologize for the inconvenience.
Thanks.

0
Comment actions Permalink

Thanks Alex, I have created an issue in the issue tracker.

0

Please sign in to leave a comment.