LINQ With Group By And rownum

本文关键字:And rownum By Group With LINQ | 更新日期: 2023-09-27 18:31:07

我有一个像下面这样的Oracle SQL查询,并希望LINQ相等。

SELECT ROWNUM rn, 
       tmp.* 
FROM   (SELECT tbl.name, 
               SUM(tbl.debit) 
        FROM   table1 tbl 
        GROUP  BY tbl.name) 

多谢

LINQ With Group By And rownum

我想你想要这样的东西:

YourDataContext.tbls.GroupBy(a => a.name)
.select((a, index) => new
{
    rn = index,
    name = a.Key,
    debitSum = a.Sum(b => b.debit)
});

我认为你可以做这样的事情,使用index选择的重载。

在这里,rn代表这个index,即...行号。

var result = <someWayToGetTable1>.GroupBy(m => m.Name)
.Select(m => new {
   name = m.Key,
   allDebit = m.Sum(x => x.debit),
})
.ToList()
.Select((m, rn) => new {
    m.name,
    m.allDebit,
    rn
});

具有部分非流畅语法的版本

 var queryable= from item in <Table1>
                 group item by item.name into g
                 select new {
                    name =m.Key,
                    allDebit = m.Sum(x => x.debit
                 }
   var result = queryable.ToList()
                 .Select((g, rn) => new {
                      g.name,
                      g.allDebit
                      rn
                  });