Row_number over (Partition by xxx order by yyy) in Linq?

Posted muz阿飞

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Row_number over (Partition by xxx order by yyy) in Linq?相关的知识,希望对你有一定的参考价值。

    public static void Main(string[] args)
    {
        var beatles = (new[] { new { id=1 , inst = "guitar" , name="john" },
                new { id=2 , inst = "guitar" , name="george" },
                new { id=3 , inst = "guitar" , name="paul" },
                new { id=4 , inst = "drums" , name="ringo" },
                new { id=5 , inst = "drums" , name="pete" }
                });
        var o = beatles
                .OrderBy(x => x.id) // order by yyy
                .GroupBy(x => x.inst)   // partition by xxx
                .Select(group => new { Group = group, Count = group.Count() })
                .SelectMany(e=>e.Group.Select(v=>v)
                    .Zip(Enumerable.Range(1,e.Count) /*ROW_NUMBER*/, (i,j)=> new {
                        i.inst,i.name, rn = j
                    })
                );
        
        foreach (var item in o)
        { 
            Console.WriteLine("{0}|{1}|{2}",item.inst,item.name, item.rn);
        }

         
        Console.ReadKey();
    }

 

以上是关于Row_number over (Partition by xxx order by yyy) in Linq?的主要内容,如果未能解决你的问题,请参考以下文章

如何在外部选择语句中使用 row_number 分区?

Oracle 根据特定属性(列)去除重复数据

Oracle中ROW_NUMBER() OVER()函数用法

ROW_NUMBER() OVER的用法

ROW_NUMBER() OVER函数的基本用法

ROW_NUMBER() OVER()函数用法详解