编程语言
首页 > 编程语言> > C# linq 实现Rank Over Partition by

C# linq 实现Rank Over Partition by

作者:互联网

public class TData

    {

       public int Id { get; set; }

       public string StuName { get; set; }

       public string ClsName { get; set; }

       public decimal Score { get; set; }

    }

 

 

   public class ToolTest

    {

        [Fact]

       public void Test()

        {

           var list = new List<TData>

            {

               new TData() { Id = 1, StuName = "zhangsan", ClsName = "1", Score = 70 },

               new TData() { Id = 2, StuName = "lisi", ClsName = "1", Score = 80 },

               new TData() { Id = 3, StuName = "wangwu", ClsName = "1", Score = 90 },

               new TData() { Id = 4, StuName = "liaoliu", ClsName = "1", Score = 100 },

               new TData() { Id = 5, StuName = "zhangsan", ClsName = "2", Score = 90 },

               new TData() { Id = 6, StuName = "lisi", ClsName = "2", Score = 40 },

               new TData() { Id = 7, StuName = "wangwu", ClsName = "2", Score = 50 },

               new TData() { Id = 8, StuName = "liaoliu", ClsName = "2", Score = 60 },

            };

 

           //var g = list.GroupBy(g => g.ClsName);

 

           var resut1 = list.GroupBy(g => g.ClsName).Select(s => s.Select(s => s.Score).Max());

 

           var resut2 = list.GroupBy(g => g.ClsName).Select(s => s.OrderByDescending(o => o.Score).FirstOrDefault());

 

        }

    }

 

标签:StuName,ClsName,C#,TData,Over,Partition,Score,new,Id
来源: https://www.cnblogs.com/hofmann/p/16640158.html