Let's say I have an Orders table and a Payments table.
Each payment relates to a specific order: Payment.orderId
I want to request my orders:
var query = from o in db.Orders where o.blah = blah select o;
But I also need the total amount for each order:
var query = from o in db.Orders where o.blah = blah select new { Order = o, totalPaid = (from p in db.Payments where p.orderId == o.id select p.Amount).Sum() };
LINQ to SQL generates exactly the SQL query that I want.
My problem is that I am adding payment support to an existing application. Therefore, to minimize the impact of code, I would like totalPaid be a property of my Order class.
I thought about adding the "manual" property and tried to populate it during the request. But the record of the select clause is where I got stuck:
var query = from o in db.Orders where o.blah = blah select "o with o.totalPaid = (from p in db.Payments <snip>).Sum()"
How can i do this?
source share