2016-03-18 82 views
0

的最新到期日期問題已得到解決。重新編輯正確的答案。我怎麼能得到與Linq

我有問題Linq。如何從數據庫中獲得截止日期。涉及到數據庫中有三個表。他們是桌子帳戶,交易,系統發票。因爲我有帳號輸入,它會得到與三個錶鏈接的結果。

一個帳戶有很多交易和系統發票。

tblAccounts.id = tbltransactions.Accountid,tbltransactions.id = tblsysteminvoices.id

我試圖找到系統的發票最新的交貨期和顯示截止日期。我需要幫助來檢查我寫錯誤的linq。我調試時得到的結果爲空。這裏是我在控制器中寫的linq。

public ActionResult Index() 
{ 
    string useracc = (string)Session["AccountNumber"]; 
    var accountInstance = db.Accounts.FirstOrDefault(w => w.AccountNumber.ToString() == useracc); 
    List<Transaction> AccountTransactions = db.Transactions.Where(w => w.AccountId == accountInstance.Id&&w.IsCancelled ==false).Select(w => w).OrderByDescending(w => w.Date).ToList(); 
    var accountStatement = AccountTransactions.Where(w => w.TransactionTypeId == 2 && w.Date.Year >= 2015).OrderByDescending(w => w.Date).ToList(); 
    var lastTransactionId = db.Transactions.Where(w => w.AccountId == accountInstance.Id && w.IsCancelled == false && w.TransactionTypeId == 2 && w.Date.Year >= 2015).Max(t => t.Id); 
    var needDueDate = db.SystemInvoices.Where(s => s.Id == lastTransactionId).Select(s => s.DueDate).FirstOrDefault(); 

    var credits = AccountTransactions.Where(w => w.Type.ToString().Contains("CREDIT")).Sum(w => w.Amount); 
    var debits = AccountTransactions.Where(w => w.Type.ToString().Contains("DEBIT")).Sum(w => w.Amount+w.Tax); 

    List<customBalanceInfoItem> currCustomBalance = new List<customBalanceInfoItem>(); 

    customBalanceInfoItem displayItem = new customBalanceInfoItem(); 
    displayItem.Balance = debits-credits; 
    displayItem.AccountNumber = accountInstance.AccountNumber; 

    currCustomBalance.Add(displayItem); 
    return View(currCustomBalance); 
} 

public partial class Account 
{ 
    public Account() 
    { 
     this.Transactions = new HashSet<Transaction>(); 
    } 
    public int Id { get; set; } 
    public string AccountNumber { get; set; } 
    public string AccountName { get; set; } 
    public virtual ICollection<Transaction> Transactions { get; set; }   
} 

public partial class Transaction 
{ 
    public Transaction() 
    { 

    } 

    public int Id { get; set; } 
    public string Type { get; set; } 
    public decimal Amount { get; set; } 
    public System.DateTime Date { get; set; } 
    public string Description { get; set; } 
    public int AccountId { get; set; } 
    public bool IsCancelled { get; set; }  
    public virtual Account Account { get; set; } 
    public virtual SystemInvoice SystemInvoice { get; set; } 

} 

public partial class SystemInvoice 
{ 
    public int Id { get; set; } 
    public System.DateTime DueDate { get; set; } 
    public decimal TotalDue { get; set; } 
    public decimal TotalDueIncTax { get; set; }  
    public virtual Transaction Transaction { get; set; } 
} 
+0

你認爲你可以發佈你的對象模型嗎? – Enigmativity

回答

0

這行看起來混亂

 var systemInvoice = db.SystemInvoices.Where(w => w.Id == accountStatement.OrderByDescending(s => s.Id).Select(s => s.Id).FirstOrDefault()).Select(w=> w); 

我想你想找到最新的交易發票。檢查以下內容。

string useracc = (string)Session["AccountNumber"]; 
    var accountInstance = db.Accounts 
          .FirstOrDefault(w => w.AccountNumber.ToString() == useracc); 
    if (accountInstance == null) { 
     return 
    }; 
    var lastTransactionId = db 
           .Transactions 
           .Where(w => w.AccountId == accountInstance.Id&&w.IsCancelled ==false); 
           .Where(w => w.TransactionTypeId == 2 && w.Date.Year >= 2015) 
           .Max(t => t.Id) 
           ; 

    var needDuedate = db.SystemInvoices 
         .Where(inv => lastTransactionId == inv.Id) 
         .OrderByDescending(inv => Inv.DueDate) 
         .select(inv => inv.DueDate) 
         .FirstOrDefault(); 
+0

感謝您的解決方案,但是當我使用var needDuedate進行調試時,我得到了Date = {1/01/0001 12:00:00 AM},還得到了錯誤信息:「無法創建類型爲'Portal.Models的常量值.Transaction'。在這種情況下,只支持原始類型或枚舉類型。「 ,有這個想法嗎? –

+0

您的意思是獲取ID等於最大交易ID的發票到期日嗎? –

+0

是的,一個transactionid =一個invoiceid,我試過以上的解決方案,仍然得到日期= {1/01/0001 12:00:00 AM}從var needDueDate –

0
List<Transaction> AccountTransactions = db.Transactions.Include("SystemInvoice") 
.Where(w => w.AccountId == accountInstance.Id&&w.IsCancelled ==false).OrderByDescending(w => w.Date).ToList(); 
    var needDuedate = AccountTransactions.Max(at=>at.SystemInvoice.DueDate); 

這能否幫助?

+0

是的,它可以過濾掉結果,但用needDueDate調試,得到Date = {1/01/0001 12:00:00 AM} –