在将我的雇主内部系统从asp迁移到asp.net MVC之前,我使用典型的发票系统作为开发MVC和ViewModel知识的测试.
我知道ViewModels是在视图中显示信息的推荐方式 – 所以我希望有一些帮助“展平”以下的viewmodel:
表:Invoice,InvoiceItem,Payment,PaymentInvoice
Invoice和InvoiceItem是链接的,Payment(记录整体付款)和PaymentInvoice(列出付款涵盖的发票)也是链接的.
我想要一个ViewModel来告诉我:
InvoiceId
顾客姓名
发票总额(数量X UnitPrice加增值税)
AmountAllocated(来自PaymentInvoice表)
杰出(TotalofInvoice – AmountAllocated)
所以我认为我的ViewModel应该是:
public class InvoiceViewModel
{
public Int InvoiceId { get; set; }
public string CustomerName { get; set; }
public decimal TotalofInvoice { get; set; }
public decimal AmountAllocated { get; set; }
public decimal Outstanding { get; set; }
}
我的域名模型是:
public class Invoice
{
public int InvoiceId { get; set; }
public int CustomerId { get; set; }
public string CustomerName { get; set; }
public string Email { get; set; }
public DateTime InvDate { get; set; }
public IList<InvoiceItem> InvoiceItems { get; set; }
}
public class InvoiceItem
{
public int InvoiceItemId { get; set; }
public int InvoiceId { get; set; }
public string Item { get; set; }
public string Description { get; set; }
public int Quantity { get; set; }
public decimal UnitPrice { get; set; }
public decimal VAT { get; set; }
public virtual Invoice Invoice { get; set; }
// calculated fields
public decimal Total
{
get { return Quantity * UnitPrice; }
}
public decimal VATAmount
{
get { return TotalPlusVAT - Total; }
}
public decimal TotalPlusVAT
{
get { return Total * (1 + VAT / 100); }
}
}
public class Payment
{
public int PaymentId { get; set; }
public int CustomerId { get; set; }
public DateTime DateReceived { get; set; }
public decimal TotalReceived { get; set; }
public IList<PaymentInvoice> PaymentInvoices { get; set; }
}
public class PaymentInvoice
{
public int PaymentInvoiceId { get; set; }
public int PaymentId { get; set; }
public decimal AmountAllocated { get; set; }
public int InvoiceId { get; set; }
public virtual Payment Payment { get; set; }
}
我的问题是如何将Payment和PaymentInvoice表链接到Invoice和InvoiceItem表,因此我可以在我的控制器中使用LINQ查询来使用“展平数据”填充viewmodel.
我也失去了LINQ查询 – 在LinqPad我得到了:
from c in Invoices
join i in InvoiceItems on c.InvoiceId equals i.InvoiceId
join pi in PaymentInvoices on c.InvoiceId equals pi.InvoiceId
select new {...into ViewModel????...}
……但不确定在那之后去哪里.
编辑 – 我最接近的是这样做的Sql是:
SELECT Invoices.InvoiceId,
Invoices.CustomerName,
(SUM(InvoiceItems.Quantity * InvoiceItems.UnitPrice)) AS TotalOfInvoice,
(SELECT SUM(AmountAllocated) AS Expr1
FROM PaymentInvoices
WHERE (InvoiceId = Invoices.InvoiceId)) AS AmountAllocated,
SUM(InvoiceItems.Quantity * InvoiceItems.UnitPrice)
- (SELECT SUM(AmountAllocated) AS Expr1
FROM PaymentInvoices
WHERE (InvoiceId = Invoices.InvoiceId)) AS Outstanding
FROM Invoices LEFT OUTER JOIN
InvoiceItems ON Invoices.InvoiceId = InvoiceItems.InvoiceId
GROUP BY Invoices.InvoiceId, Invoices.CustomerName
谢谢,
标记
最佳答案 我认为您的Linq查询几乎是完美的,您只需要选择新的ViewModel:
from c in Invoices
join i in InvoiceItems on c.InvoiceId equals i.InvoiceId
join pi in PaymentInvoices on c.InvoiceId equals pi.InvoiceId
select new InvoiceViewModel {
InvoiceId = c.InvoiceId,
CustomerName = c.CustomerName,
TotalofInvoice = c.InvoiceItems.Sum(invoiceitem => invoiceitem.Total(),
AmountAllocated = ...
Outstanding = ...
};