Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query: Aggregate function over subquery produces incorrect SQL #3792

Closed
GunterGladDC opened this issue Nov 19, 2015 · 3 comments
Closed

Query: Aggregate function over subquery produces incorrect SQL #3792

GunterGladDC opened this issue Nov 19, 2015 · 3 comments
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@GunterGladDC
Copy link

When I run this select:

var list6 = context.Customers.Select(x => new
         {
            Name = x.Name,
            InvoiceTotal = x.Invoices.Sum(p => p.InvoiceDetails.Sum(d => d.TotalAmount)),
            PaymentTotal = x.Payments.Sum(p => p.PaymentAmount)
         }
         ).ToList();

I get this error:
Cannot perform an aggregate function on an expression containing an aggregate or a subquery.

My DB context:

public class AppDBContext: DbContext, { 
public DbSet<Customer> Customers { get; set; }
public DbSet<Invoice> Invoices { get; set; }
public DbSet<InvoiceDetail>  InvoiceDetails { get; set; }
}

public class Customer 
{
public string Name { get; set; }
public virtual ICollection<Invoice> Invoices { get; set; }
}

public class Invoice
{
#region Customer
public int CustomerID { get; set; }
public virtual Customer Customer { get; set; }
#endregion Customer

public virtual ICollection<InvoiceDetail> InvoiceDetails { get; set; }
}

public class InvoiceDetail 
   {
      public int InvoiceID { get; set; }
      public decimal TotalAmount { get; set; }

      public virtual Invoice Invoice { get; set; }
   }
@natemcmaster
Copy link
Contributor

@mikary
Copy link
Contributor

mikary commented Apr 27, 2016

Still appears to repro on current dev.

Cleaned up version of repro:

    class Program
    {
        static void Main(string[] args)
        {
            using (var context = new MyContext())
            {
                context.Database.EnsureDeleted();
                context.Database.EnsureCreated();

                var list6 = context.Customers.Select(x => new
                {
                    Name = x.Name,
                    InvoiceTotal = x.Invoices.Sum(p => p.InvoiceDetails.Sum(d => d.TotalAmount)),
                    PaymentTotal = x.Payments.Sum(p => p.PaymentAmount)
                }
                    ).ToList();
            }
        }
    }

    public class MyContext : DbContext
    {
        public DbSet<Customer> Customers { get; set; }
        public DbSet<Invoice> Invoices { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer(
                "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=NestedSum;Integrated Security=True");
        }
    }

    public class Customer
    {
        public int CustomerID { get; set; }

        public string Name { get; set; }
        public virtual ICollection<Invoice> Invoices { get; set; }

        public virtual ICollection<Payment> Payments { get; set; }
    }

    public class Invoice
    {
        public int InvoiceID { get; set; }

        public int CustomerID { get; set; }
        public virtual Customer Customer { get; set; }

        public virtual ICollection<InvoiceDetail> InvoiceDetails { get; set; }
    }

    public class InvoiceDetail
    {
        public int InvoiceDetailID { get; set; }

        public decimal TotalAmount { get; set; }

        public int InvoiceID { get; set; }
        public virtual Invoice Invoice { get; set; }
    }

    public class Payment
    {
        public int PaymentID { get; set; }
        public virtual decimal PaymentAmount { get; set; }

        public int CustomerID { get; set; }
        public virtual Customer Customer { get; set; }
    }

@rowanmiller rowanmiller modified the milestones: 1.0.1, 1.0.0 May 4, 2016
@rowanmiller rowanmiller removed the pri0 label Jul 6, 2016
@maumar maumar assigned smitpatel and unassigned anpete Oct 5, 2016
@maumar maumar modified the milestones: 1.2.0, 1.1.0-preview1 Oct 5, 2016
@smitpatel smitpatel changed the title Unable to perform nested sum. EF7.0.0-RC2-16332 Query: Sum over subquery produces incorrect SQL Oct 6, 2016
@tuespetre
Copy link
Contributor

Related: #800

@smitpatel smitpatel added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Feb 24, 2017
@smitpatel smitpatel changed the title Query: Sum over subquery produces incorrect SQL Query: Aggregate function over subquery produces incorrect SQL Feb 24, 2017
@divega divega added closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. and removed closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. labels May 10, 2017
@ajcvickers ajcvickers modified the milestones: 2.0.0-preview1, 2.0.0 Oct 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Projects
None yet
Development

No branches or pull requests

10 participants