我需要对数据库表(Period)的时间段和表(发票)中的发票列表执行LINQ查询,这些发票列在期间的开始和结束日期之内。这两个表之间没有关键引用,如何进行Invoice子查询?
我正在尝试做类似以下的事情:
var query = (from p in db.DataContext.Periods
// Subquery i in db.DataContext.Invoices
let InvoiceAmount = i.Where(t => t.InvoiceDate >= p.StartDate && t.InvoiceDate <= p.EndDate)
select new PeriodView
(
p.Name,
p.StartDate,
p.EndDate,
InvoiceAmount.Count()
));
答案 0 :(得分:4)
var periodViewList =
(from p in db.DataContext.Periods
select new PeriodView(
p.Name,
p.StartDate,
p.EndDate,
db.DataContext.Invoices.Where(i => i.InvoiceDate >= p.StartDate && i.InvoiceDate <= p.EndDate).Count()
)).ToList();
我假设PeriodView构造函数看起来像这样
public PeriodView (string name, DateTime startDate, DateTime endDate, int invoiceCount) {
...
}