-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
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
See also https://github.com/aspnet/MusicStore/blob/dev/src/MusicStore/Components/GenreMenuComponent.cs#L31 (Using EF 1.0.0-rc2-20051) |
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; }
} |
Related: #800 |
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
When I run this select:
I get this error:
Cannot perform an aggregate function on an expression containing an aggregate or a subquery.
My DB context:
The text was updated successfully, but these errors were encountered: