Entity Framework get SUM from child property

1

I have the following model where I'd like to get the sum of all OrderTotalItems for all Orders of a Customer where the OrderTotalType (Enumeration) is "total" or 99:

public class Customer
{
    ...
    public ICollection<Order> Orders { get; set; } = new Collection<Order>();
}

public class Order
{
    ...
    public ICollection<OrderTotalItem> OrderTotalItems { get; set; } = new Collection<OrderTotalItem>();
}

public class OrderTotalItem
{
    [Required]
    public int Id { get; set; }
    [Required]
    [Column(TypeName = "decimal(10, 4)")]
    public decimal Value { get; set; }
    [Required]
    public OrderTotalType Type { get; set; }
}

I am building a CustomerAdminDTO to include all relevant data of a customer for the admin client:

public class CustomerAdminDto
{
    public int Id { get; set; }
    public string UserId { get; set; }
    public Gender Gender { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string VATId { get; set; } = "";
    public bool VATIdValid { get; set; } = false;
    public DateTime Added { get; set; }
    public DateTime LastModified { get; set; }
    public decimal OrdersTotal { get; set; }
    public CustomerStatusShortDto CustomerStatus { get; set; }
    public CustomerAddressDto CustomerAddress { get; set; }
    public CustomerAddressDto BillingAddress { get; set; }
    public ICollection<OrderListShortDto> Orders { get; set; }
}

In my data service I fill the DTO like that

var customerAdmin = await _context.Customers
    .Include(x => x.Addresses)
    .Include(x => x.CustomerStatus)
    .Include(x => x.Orders)
        .ThenInclude(x => x.OrderTotalItems)
    .Where(x => x.UserId == userid)
    .Select(customer => new CustomerAdminDto 
    {
        Id = customer.Id,
        UserId = customer.UserId,
        Gender = customer.Gender,
        FirstName = customer.FirstName,
        LastName = customer.LastName,
        VATId = customer.VATId,
        VATIdValid = customer.VATIdValid,
        Added = customer.Added,
        LastModified = customer.LastModified,
        OrdersTotal = customer.Orders.Sum(x => x.OrderTotalItems
            .Where(x => x.Type == Enums.OrderTotalType.Total)
            .Sum(x => x.Value)),
        CustomerStatus = new CustomerStatusShortDto
        {
            Id = customer.CustomerStatus.Id,
            Name = customer.CustomerStatus.Name,
        },
    ...
    }
    .FirstOrDefaultAsync();

where everything works, except the OrdersTotal.

API compiles fine but throws the following error at runtime:

Microsoft.Data.SqlClient.SqlException (0x80131904): Cannot perform an aggregate function on an expression containing an aggregate or a subquery.

Thanks for your hints!

c#
entity-framework
entity-framework-core
asked on Stack Overflow Apr 13, 2020 by Igotcha • edited Apr 13, 2020 by marc_s

1 Answer

1

Cannot perform an aggregate function on an expression containing an aggregate or a subquery.

This error in SQL server means that you tried to call aggregation function (customer.Orders.Sum() in your case) on other expression that contains aggregation function (.Sum(x => x.Value) in your case). In order to avoid this you can simplify your LINQ expression for OrdersTotal:

OrdersTotal = customer.Orders.SelectMany(o => o.OrderTotalItems).Where(x => x.Type == Enums.OrderTotalType.Total).Sum(x => x.Value)

There is only one aggregation here so it should work fine

answered on Stack Overflow Apr 13, 2020 by ingvar

User contributions licensed under CC BY-SA 3.0