如何使用linq查询检索上一行值?

时间:2013-11-05 06:11:07

标签: c#

我想检索transnett和obnett的前一行值,以便根据StackHistoryID更新当前行,以便上述查询是否有效?

var val = (from tn in db.tablename.ToList()
           where tn.ID.ToString() != currentID.ToString()
           orderby tn.TransactionDate descending
           group tn by tn.StackHistoryID into trans
           select new {
               transnett=trans.Select(t=>t.TransNett),
               obnett=trans.Select(t=>t.OBNett)
           }).FirstOrDefault();

1 个答案:

答案 0 :(得分:0)

您的查询返回最新行,假设TransactionDate始终具有值且已正确设置。这是你想要的,还是你想要之前的行?

我认为您的查询看起来更像:

var rows = from tn in db.tablename
           where tn.ID != currentID
           orderby tn.TransactionDate descending
           group tn by tn.StackHistoryID into trans
           select new {transnett=trans.Select(t=>t.TransNett),obnett=trans.Select(t=>t.OBNett)});

var previousRow = rows.first();