簡體   English   中英

linq檢索名稱而不是id並按降序列出

[英]linq to retrieve name instead of id and list in descending order

你能幫我解決這個問題嗎? 我正在檢索每個頭的余額,並且已檢索每個頭的余額。 現在,我想按降序列出余額,並列出名稱而不是h_id。 我用了代碼

protected void account_watchlist() {
  using(var context = new sem_dbEntities()) {
    //ledger && head
    var year = DateTime.Now.AddMinutes(318).Year;
    var month = DateTime.Now.AddMinutes(318).Month;
    var start = new DateTime();
    if (month >= 4) {
      start = new DateTime(year, 04, 01);
    } else if (month < 4) {
      start = new DateTime(year - 1, 04, 01);
    }
    var qr = (from a in context.ledgers
              where a.entry_date >= start && a.entry_date < new DateTime(year, month, 1) 
              join b in context.heads on a.h_id equals b.h_id
              group a by a.h_id into sb select new {
        sb.FirstOrDefault().h_id,
          totalc = sb.Sum(c => c.credit),
          totald = sb.Sum(d => d.debit),
          balance = sb.Sum(d => d.debit) - sb.Sum(c => c.credit)
      }).ToList();
    Repeater2.DataSource = qr.ToList();
    Repeater2.DataBind();
  }
}

您需要將分類帳與組長組合使用。 它將使您可以訪問總公司實體和所有相關分類帳(在headLedgers集合中):

from h in context.heads
join l in context.ledgers.Where(x => x.entry_date >= startDate && x.entry_date < endDate)
   on h.h_id equals l.h_id into headLedgers
where headLedgers.Any()
let totalc = headLedgers.Sum(l => l.credit),
let totald = headLedgers.Sum(l => l.debit),  
select new {
   h.h_id,
   h.name,
   totalc,
   totald,
   balance = totald - totalc,
}

我還為總貸方和總借方引入了兩個范圍變量(請在此處考慮更好的名稱),以避免第二次計算余額。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM