RowNumber使用Linq

时间:2014-10-23 04:22:45

标签: c# sql linq entity-framework

如何使用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

3 个答案:

答案 0 :(得分:0)

试试这个:

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

答案 1 :(得分:0)

这是一个很好的例子:

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();

答案 2 :(得分:0)

var int = 1;
var query = from x in db.ZEM_LIC_UCH
            order by x.SERRIA_NUMBER
            select new {
                RowNumber = i++,
                DATE_REG_END = x.DATE_REG_END,
                DATE_CHANGE = x.DATE_CHANGE
            };