RowNumber using Linq

本文关键字:Linq using RowNumber | 更新日期: 2023-09-27 18:27:28

如何使用Linq执行这个sql查询?

SELECT ROW_NUMBER() OVER(partition by T.SERRIA_NUMBER order by T.SERRIA_NUMBER),
       T.DATE_REG_END, 
       T.DATE_CHANGE
  FROM ZEM_LIC_UCH T

RowNumber using Linq

试试这个:

var query=db.ZEM_LIC_UCH.OrderBy(t=>t.SERRIA_NUMBER)
                        .Select((t,i)=>new
                             {
                               row=i,
                               t.DATE_REG_END, 
                               t.DATE_CHANGE
                             });

这里有一个很好的例子:

var query = db.ZEM_LIC_UCH
               .OrderBy(o => o.SERRIA_NUMBER)
               .GroupBy(g => g.SERRIA_NUMBER)
               .Select(s => new { s, Count = s.Count() })
               .SelectMany(sm => sm.s.Select(s => s)
                  .Zip(Enumerable.Range(1, sm.Count), (row, index) => 
                  new { 
                     rn = index, 
                     row.SERRIA_NUMBER, 
                     row.DATE_REG_END, 
                     row.DATE_CHANGE 
                  }))
               .ToList();
var tmpInt = 1;
var query = from x in db.ZEM_LIC_UCH
            order by x.SERRIA_NUMBER
            select new {
                RowNumber = tmpInt++,
                DATE_REG_END = x.DATE_REG_END,
                DATE_CHANGE = x.DATE_CHANGE
            };