在處理將我的僱主內部系統從asp遷移到asp.net MVC之前,我使用典型的發票系統作爲開發MVC和ViewModel知識的測試。ViewModel和LINQ查詢代表Asp.Net中的4個鏈接表MVC
我知道的ViewModels是在視圖中顯示的信息的推薦方法 - 所以我希望一些幫助「扁平化」的視圖模型爲以下幾點:
表:發票,InvoiceItem,付款,PaymentInvoice
已將發票和InvoiceItem關聯起來,並且支付(記錄整體支付)和PaymentInvoice(列出哪些發票支付涵蓋)也是鏈接的。
我想一個視圖模型給我看:
InvoiceId 客戶名稱 發票(數量X單價(不含稅)) AmountAllocated(從PaymentInvoice表) 傑出(TotalofInvoice - AmountAllocated)的總
所以我覺得我的視圖模型應該是:
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; }
}
我的問題是如何支付和PaymentInvoice表,發票和InvoiceItem錶鏈接,這樣我就可以使用LINQ查詢在我的控制器與「扁平化數據」來填充視圖模型。
我與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
謝謝
馬克
你和你的區別SQL和LINQ查詢是「INNER JOIN」與「LEFT OUTER JOIN」。做一些谷歌上搜索「LINQ左外連接」來獲取一些例子,像http://msdn.microsoft.com/en-us/library/vstudio/bb397895.aspx –
感謝 - 使用,我知道了顯示個別項目,但我正在尋找在查詢總結項目 - 什麼我現在是:從發票VAR INVS =發票 在invoice.InvoiceId InvoiceItems加盟項目從等於item.InvoiceId到GJ \t \t \t \t \t子項目gj.DefaultIfEmpty() \t \t \t \t \t select new {invoice.CustomerName,costs =(subitem == null?0:subitem。UnitPrice) \t \t \t \t \t}; (剛剛嘗試一個鏈接,看看我能否得到它的工作) – Mark