我怎样才能获得与Linq

时间:2016-03-18 00:27:07

标签: c# asp.net-mvc linq

问题已得到解决。重新编辑正确的答案。

我遇到了Linq的问题。如何从db获取截止日期。涉及数据库中有三个表。它们是表帐户,交易,系统发票。由于我有帐号输入,它会将结果与三个表联系起来。

一个帐户有许多交易和系统发票。

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

我正在尝试在系统发票中找到最新的DueDate并显示截止日期。如果错误,我需要帮助来检查我写的linq。我调试的结果是null。这是我在控制器中写的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; }
}

2 个答案:

答案 0 :(得分: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();

答案 1 :(得分: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);

这会有帮助吗?