Problem with complex LINQ query

May 7, 2012 at 9:05 PM

Using stylecop build 4.7.19.0 in Visual Studio 2010 Ultimate, here is simple example of a LINQ query which gives rise to errors SA1500/SA1104. The original LINQ expression was some 63 lines long.

The alternatives that I have been able to find also have errors.

    /// <summary>
    /// Run a summary.
    /// </summary>
    /// <remarks>awaiting remarks</remarks>
    [Test]
    public void Summary1()
    {
      var dataTable = TestData.Load1();
      //// The line "} into g" give the following
      //// Warning 1 SA1500: If a statement spans multiple lines, the closing curly bracket must be placed on its own line.
      //// Warning 2 SA1104: A query clause must begin on a new line if the previous clause spans multiple lines.
      var summary = (from c in dataTable
                     group c by new
                       {
                         c.MassE,
                         c.Name
                       } into g
                       orderby g.Key.MassE, g.Key.Name
                       select new
                         {
                           g.Key.MassE,
                           g.Key.Name,
                           SumMoons = g.Sum(c => c.Moons)
                         }).Distinct();
      foreach (var variable in summary)
      {
        Console.WriteLine(variable.MassE + " " + variable.Name);
      }
    }

    /// <summary>
    /// Run a summary.
    /// </summary>
    /// <remarks>awaiting remarks</remarks>
    [Test]
    public void Summary2()
    {
      var dataTable = TestData.Load1();
      //// The line "} into g" give the following
      //// Warning 2 SA1513: Statements or elements wrapped in curly brackets must be followed by a blank line.
      var summary = (from c in dataTable
                     group c by new
                     {
                       c.MassE,
                       c.Name
                     }
                     into g
                     orderby g.Key.MassE, g.Key.Name
                     select new
                     {
                       g.Key.MassE,
                       g.Key.Name,
                       SumMoons = g.Sum(c => c.Moons)
                     }).Distinct();
      foreach (var variable in summary)
      {
        Console.WriteLine(variable.MassE + " " + variable.Name);
      }
    }

    /// <summary>
    /// Run a summary.
    /// </summary>
    /// <remarks>awaiting remarks</remarks>
    [Test]
    public void Summary3()
    {
      var dataTable = TestData.Load1();
      //// The line "} into g" give the following
      //// Warning 4 SA1102: The query clause must begin on the line after the previous clause, with no blank lines in between.
      var summary = (from c in dataTable
                     group c by new
                     {
                       c.MassE,
                       c.Name
                     }

                       into g
                       orderby g.Key.MassE, g.Key.Name
                       select new
                       {
                         g.Key.MassE,
                         g.Key.Name,
                         SumMoons = g.Sum(c => c.Moons)
                       }).Distinct();
      foreach (var variable in summary)
      {
        Console.WriteLine(variable.MassE + " " + variable.Name);
      }
    }

May 7, 2012 at 9:06 PM
Edited May 8, 2012 at 11:14 AM

I have a complete solution that I can email to you to illustrate the problem.

Coordinator
May 8, 2012 at 10:18 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.