的最新到期日期問題已得到解決。重新編輯正確的答案。我怎麼能得到與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; }
}
你認爲你可以發佈你的對象模型嗎? – Enigmativity