码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-09-30 16:29:19      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:[]   orderby   nbsp   art   count   sel   style   code   range   

    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?

标签:[]   orderby   nbsp   art   count   sel   style   code   range   

原文地址:http://www.cnblogs.com/leeafei/p/7615206.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!