I have the following classes
public class Order
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public DateTime Date { get; set; }
public decimal TotalPaid { get { return Payments.Sum(x => x.Amount); } }
public decimal Total { get { return OrderItems.Sum(x => x.TotalPrice); } }
public bool PaidCompletely { get { return Total == TotalPaid; } }
public List<OrderItem> OrderItems { get; set; } = new List<OrderItem>();
public List<Payment> Payments { get; set; } = new List<Payment>();
}
public class OrderItem
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public decimal PricePerUnit { get; set; }
public decimal Quantity { get; set; }
public decimal TotalPrice { get { return PricePerUnit * Quantity; } }
}
public class Payment
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public decimal Amount { get; set; }
public DateTime Date { get; set; }
}
I want to get all orders that are not fully paid.
The following is not working
var OrdersNotFullyPaidShort =
context
.Orders
.Where(order => !order.PaidCompletely)
.ToList();
giving the error
The LINQ expression 'Where(source: DbSet, predicate: (o) => !(o.PaidCompletely))' could not be translated...
The following works, but I have to rewrite all the logic again in the query, while I have the logic also defined in the classes:
var OrdersNotFullyPaidLong =
context
.Orders
.Where(order => order.OrderItems.Sum(orderItem => orderItem.PricePerUnit * orderItem.Quantity) == order.Payments.Sum(payment => payment.Amount))
.ToList();
and generates this quite ugly query:
SELECT [o].[Id], [o].[Date]
FROM [Orders] AS [o]
WHERE (((
SELECT SUM([o0].[PricePerUnit] * [o0].[Quantity])
FROM [OrderItems] AS [o0]
WHERE ([o].[Id] = [o0].[OrderId]) AND [o0].[OrderId] IS NOT NULL) = (
SELECT SUM([p].[Amount])
FROM [Payments] AS [p]
WHERE ([o].[Id] = [p].[OrderId]) AND [p].[OrderId] IS NOT NULL)) AND ((
SELECT SUM([o0].[PricePerUnit] * [o0].[Quantity])
FROM [OrderItems] AS [o0]
WHERE ([o].[Id] = [o0].[OrderId]) AND [o0].[OrderId] IS NOT NULL) IS NOT NULL AND (
SELECT SUM([p].[Amount])
FROM [Payments] AS [p]
WHERE ([o].[Id] = [p].[OrderId]) AND [p].[OrderId] IS NOT NULL) IS NOT NULL)) OR ((
SELECT SUM([o0].[PricePerUnit] * [o0].[Quantity])
FROM [OrderItems] AS [o0]
WHERE ([o].[Id] = [o0].[OrderId]) AND [o0].[OrderId] IS NOT NULL) IS NULL AND (
SELECT SUM([p].[Amount])
FROM [Payments] AS [p]
WHERE ([o].[Id] = [p].[OrderId]) AND [p].[OrderId] IS NOT NULL) IS NULL)
Is there no way to use my first way of querying? What am I doing wrong, are my classes not well defined?
.Where(order => !order.PaidCompletely)
, but.Where(order => OrderItems.Sum(x => x.TotalPrice))-Payments.Sum(x => x.Amount)>0)
This shall just demonstrate the approach, it's not the final solution. In this way, LINQ has the chance, at least to receive the Formulas. LINQ is done at runtime there is no Source available, only the compiled machine code. LINQ cannot translate machine code to SQL. Inside a Lambda, the code is not immediatly machine code, it's an Expession<Func<>>, that's kind of stored source code.No, you have to form a Query in One Place, in one line
Well, that isn't strictly speaking true. It can be over multiple lines of code. But I agree with the rest of the comment @Holger.