我正在使用典型的发票系统作为开发MVC和ViewModel知识的测试,然后将我的雇主内部系统从asp迁移到asp.net MVC。
我知道ViewModels是在视图中显示信息的推荐方式 - 所以我希望帮助“展平”以下的viewmodel:
表:Invoice,InvoiceItem,Payment,PaymentInvoice
发票和InvoiceItem已关联,付款(记录整体付款)和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
谢谢,
标记
答案 0 :(得分:2)
我认为您的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 = ...
};