{newStudentScore{I" />

亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

Linq中GroupBy方法的使用總結

系統 2070 0

? Demo模型類:

      
        public
      
      
        class
      
      
         StudentScore

{



    
      
      
        public
      
      
        int
      
       ID { 
      
        set
      
      ; 
      
        get
      
      
        ; }



    
      
      
        public
      
      
        string
      
       Name { 
      
        set
      
      ; 
      
        get
      
      
        ; }



    
      
      
        public
      
      
        string
      
       Course { 
      
        set
      
      ; 
      
        get
      
      
        ; }



    
      
      
        public
      
      
        int
      
       Score { 
      
        set
      
      ; 
      
        get
      
      
        ; }



    
      
      
        public
      
      
        string
      
       Term { 
      
        set
      
      ; 
      
        get
      
      
        ; }



}
      
    

Demo示例代碼:

      
        static
      
      
        void
      
      
         Main()

        {

            
      
      
        var
      
       lst = 
      
        new
      
       List<StudentScore>
      
        

                          {

                              
      
      
        new
      
       StudentScore {ID = 
      
        1
      
      , Name = 
      
        "
      
      
        張三
      
      
        "
      
      , Term = 
      
        "
      
      
        第一學期
      
      
        "
      
      , Course = 
      
        "
      
      
        Math
      
      
        "
      
      , Score = 
      
        80
      
      
        },

                              
      
      
        new
      
       StudentScore {ID = 
      
        1
      
      , Name = 
      
        "
      
      
        張三
      
      
        "
      
      , Term = 
      
        "
      
      
        第一學期
      
      
        "
      
      , Course = 
      
        "
      
      
        Chinese
      
      
        "
      
      , Score = 
      
        90
      
      
        },

                              
      
      
        new
      
       StudentScore {ID = 
      
        1
      
      , Name = 
      
        "
      
      
        張三
      
      
        "
      
      , Term = 
      
        "
      
      
        第一學期
      
      
        "
      
      , Course = 
      
        "
      
      
        English
      
      
        "
      
      , Score = 
      
        70
      
      
        },

                              
      
      
        new
      
       StudentScore {ID = 
      
        2
      
      , Name = 
      
        "
      
      
        李四
      
      
        "
      
      , Term = 
      
        "
      
      
        第一學期
      
      
        "
      
      , Course = 
      
        "
      
      
        Math
      
      
        "
      
      , Score = 
      
        60
      
      
        },

                              
      
      
        new
      
       StudentScore {ID = 
      
        2
      
      , Name = 
      
        "
      
      
        李四
      
      
        "
      
      , Term = 
      
        "
      
      
        第一學期
      
      
        "
      
      , Course = 
      
        "
      
      
        Chinese
      
      
        "
      
      , Score = 
      
        70
      
      
        },

                              
      
      
        new
      
       StudentScore {ID = 
      
        2
      
      , Name = 
      
        "
      
      
        李四
      
      
        "
      
      , Term = 
      
        "
      
      
        第一學期
      
      
        "
      
      , Course = 
      
        "
      
      
        English
      
      
        "
      
      , Score = 
      
        30
      
      
        },

                              
      
      
        new
      
       StudentScore {ID = 
      
        3
      
      , Name = 
      
        "
      
      
        王五
      
      
        "
      
      , Term = 
      
        "
      
      
        第一學期
      
      
        "
      
      , Course = 
      
        "
      
      
        Math
      
      
        "
      
      , Score = 
      
        100
      
      
        },

                              
      
      
        new
      
       StudentScore {ID = 
      
        3
      
      , Name = 
      
        "
      
      
        王五
      
      
        "
      
      , Term = 
      
        "
      
      
        第一學期
      
      
        "
      
      , Course = 
      
        "
      
      
        Chinese
      
      
        "
      
      , Score = 
      
        80
      
      
        },

                              
      
      
        new
      
       StudentScore {ID = 
      
        3
      
      , Name = 
      
        "
      
      
        王五
      
      
        "
      
      , Term = 
      
        "
      
      
        第一學期
      
      
        "
      
      , Course = 
      
        "
      
      
        English
      
      
        "
      
      , Score = 
      
        80
      
      
        },

                              
      
      
        new
      
       StudentScore {ID = 
      
        4
      
      , Name = 
      
        "
      
      
        趙六
      
      
        "
      
      , Term = 
      
        "
      
      
        第一學期
      
      
        "
      
      , Course = 
      
        "
      
      
        Math
      
      
        "
      
      , Score = 
      
        90
      
      
        },

                              
      
      
        new
      
       StudentScore {ID = 
      
        4
      
      , Name = 
      
        "
      
      
        趙六
      
      
        "
      
      , Term = 
      
        "
      
      
        第一學期
      
      
        "
      
      , Course = 
      
        "
      
      
        Chinese
      
      
        "
      
      , Score = 
      
        80
      
      
        },

                              
      
      
        new
      
       StudentScore {ID = 
      
        4
      
      , Name = 
      
        "
      
      
        趙六
      
      
        "
      
      , Term = 
      
        "
      
      
        第一學期
      
      
        "
      
      , Course = 
      
        "
      
      
        English
      
      
        "
      
      , Score = 
      
        70
      
      
        },

                              
      
      
        new
      
       StudentScore {ID = 
      
        1
      
      , Name = 
      
        "
      
      
        張三
      
      
        "
      
      , Term = 
      
        "
      
      
        第二學期
      
      
        "
      
      , Course = 
      
        "
      
      
        Math
      
      
        "
      
      , Score = 
      
        100
      
      
        },

                              
      
      
        new
      
       StudentScore {ID = 
      
        1
      
      , Name = 
      
        "
      
      
        張三
      
      
        "
      
      , Term = 
      
        "
      
      
        第二學期
      
      
        "
      
      , Course = 
      
        "
      
      
        Chinese
      
      
        "
      
      , Score = 
      
        80
      
      
        },

                              
      
      
        new
      
       StudentScore {ID = 
      
        1
      
      , Name = 
      
        "
      
      
        張三
      
      
        "
      
      , Term = 
      
        "
      
      
        第二學期
      
      
        "
      
      , Course = 
      
        "
      
      
        English
      
      
        "
      
      , Score = 
      
        70
      
      
        },

                              
      
      
        new
      
       StudentScore {ID = 
      
        2
      
      , Name = 
      
        "
      
      
        李四
      
      
        "
      
      , Term = 
      
        "
      
      
        第二學期
      
      
        "
      
      , Course = 
      
        "
      
      
        Math
      
      
        "
      
      , Score = 
      
        90
      
      
        },

                              
      
      
        new
      
       StudentScore {ID = 
      
        2
      
      , Name = 
      
        "
      
      
        李四
      
      
        "
      
      , Term = 
      
        "
      
      
        第二學期
      
      
        "
      
      , Course = 
      
        "
      
      
        Chinese
      
      
        "
      
      , Score = 
      
        50
      
      
        },

                              
      
      
        new
      
       StudentScore {ID = 
      
        2
      
      , Name = 
      
        "
      
      
        李四
      
      
        "
      
      , Term = 
      
        "
      
      
        第二學期
      
      
        "
      
      , Course = 
      
        "
      
      
        English
      
      
        "
      
      , Score = 
      
        80
      
      
        },

                              
      
      
        new
      
       StudentScore {ID = 
      
        3
      
      , Name = 
      
        "
      
      
        王五
      
      
        "
      
      , Term = 
      
        "
      
      
        第二學期
      
      
        "
      
      , Course = 
      
        "
      
      
        Math
      
      
        "
      
      , Score = 
      
        90
      
      
        },

                              
      
      
        new
      
       StudentScore {ID = 
      
        3
      
      , Name = 
      
        "
      
      
        王五
      
      
        "
      
      , Term = 
      
        "
      
      
        第二學期
      
      
        "
      
      , Course = 
      
        "
      
      
        Chinese
      
      
        "
      
      , Score = 
      
        70
      
      
        },

                              
      
      
        new
      
       StudentScore {ID = 
      
        3
      
      , Name = 
      
        "
      
      
        王五
      
      
        "
      
      , Term = 
      
        "
      
      
        第二學期
      
      
        "
      
      , Course = 
      
        "
      
      
        English
      
      
        "
      
      , Score = 
      
        80
      
      
        },

                              
      
      
        new
      
       StudentScore {ID = 
      
        4
      
      , Name = 
      
        "
      
      
        趙六
      
      
        "
      
      , Term = 
      
        "
      
      
        第二學期
      
      
        "
      
      , Course = 
      
        "
      
      
        Math
      
      
        "
      
      , Score = 
      
        70
      
      
        },

                              
      
      
        new
      
       StudentScore {ID = 
      
        4
      
      , Name = 
      
        "
      
      
        趙六
      
      
        "
      
      , Term = 
      
        "
      
      
        第二學期
      
      
        "
      
      , Course = 
      
        "
      
      
        Chinese
      
      
        "
      
      , Score = 
      
        60
      
      
        },

                              
      
      
        new
      
       StudentScore {ID = 
      
        4
      
      , Name = 
      
        "
      
      
        趙六
      
      
        "
      
      , Term = 
      
        "
      
      
        第二學期
      
      
        "
      
      , Course = 
      
        "
      
      
        English
      
      
        "
      
      , Score = 
      
        70
      
      
        },

                          };
      
    

?

      
        //
      
      
        分組,根據姓名,統計Sum的分數,統計結果放在匿名對象中。兩種寫法。 

            
      
      
        //
      
      
        第一種寫法 
      
      

            Console.WriteLine(
      
        "
      
      
        ---------第一種寫法
      
      
        "
      
      
        );

            
      
      
        var
      
       studentSumScore_1 = (
      
        from
      
       l 
      
        in
      
      
         lst

                                     group l by l.Name

                                     into grouped

                                     
      
      
        orderby
      
       grouped.Sum(m =>
      
         m.Score)

                                     
      
      
        select
      
      
        new
      
       {Name = grouped.Key, Scores = grouped.Sum(m =>
      
         m.Score)}).ToList();

            
      
      
        foreach
      
       (
      
        var
      
       l 
      
        in
      
      
         studentSumScore_1)

            {

                Console.WriteLine(
      
      
        "
      
      
        {0}:總分{1}
      
      
        "
      
      
        , l.Name, l.Scores);

            }

  

            
      
      
        //
      
      
        第二種寫法 
      
      

            Console.WriteLine(
      
        "
      
      
        ---------第二種寫法
      
      
        "
      
      
        );

            
      
      
        var
      
       studentSumScore_2 = lst.GroupBy(m =>
      
         m.Name)

                .Select(k 
      
      => 
      
        new
      
       {Name = k.Key, Scores = k.Sum(l =>
      
         l.Score)})

                .OrderBy(m 
      
      =>
      
         m.Scores).ToList();

            
      
      
        foreach
      
       (
      
        var
      
       l 
      
        in
      
      
         studentSumScore_2)

            {

                Console.WriteLine(
      
      
        "
      
      
        {0}:總分{1}
      
      
        "
      
      
        , l.Name, l.Scores);

            }

  

            
      
      
        //
      
      
        分組,根據2個條件學期和課程,統計各科均分,統計結果放在匿名對象中。兩種寫法。 
      
      

            Console.WriteLine(
      
        "
      
      
        ---------第一種寫法
      
      
        "
      
      
        );

            
      
      
        var
      
       TermAvgScore_1 = (
      
        from
      
       l 
      
        in
      
      
         lst

                                  group l by 
      
      
        new
      
      
         {l.Term, l.Course}

                                  into grouped

                                  
      
      
        orderby
      
       grouped.Average(m =>
      
         m.Score) ascending

                                  
      
      
        orderby
      
      
         grouped.Key.Term descending

                                  
      
      
        select
      
      
        new
      
       {grouped.Key.Term, grouped.Key.Course, Scores = grouped.Average(m =>
      
         m.Score)})

                .ToList();

            
      
      
        foreach
      
       (
      
        var
      
       l 
      
        in
      
      
         TermAvgScore_1)

            {

                Console.WriteLine(
      
      
        "
      
      
        學期:{0},課程:{1},均分:{2}
      
      
        "
      
      
        , l.Term, l.Course, l.Scores);

            }

            Console.WriteLine(
      
      
        "
      
      
        ---------第二種寫法
      
      
        "
      
      
        );

            
      
      
        var
      
       TermAvgScore_2 = lst.GroupBy(m => 
      
        new
      
      
         {m.Term, m.Course})

                .Select(k 
      
      => 
      
        new
      
       {k.Key.Term, k.Key.Course, Scores = k.Average(m =>
      
         m.Score)})

                .OrderBy(l 
      
      => l.Scores).ThenByDescending(l =>
      
         l.Term);

            
      
      
        foreach
      
       (
      
        var
      
       l 
      
        in
      
      
         TermAvgScore_2)

            {

                Console.WriteLine(
      
      
        "
      
      
        學期:{0},課程:{1},均分:{2}
      
      
        "
      
      
        , l.Term, l.Course, l.Scores);

            }

  

            
      
      
        //
      
      
        分組,帶有Having的查詢,查詢均分>80的學生 
      
      

            Console.WriteLine(
      
        "
      
      
        ---------第一種寫法
      
      
        "
      
      
        );

            
      
      
        var
      
       AvgScoreGreater80_1 = (
      
        from
      
       l 
      
        in
      
      
         lst

                                       group l by 
      
      
        new
      
      
         {l.Name, l.Term}

                                       into grouped

                                       
      
      
        where
      
       grouped.Average(m => m.Score) >= 
      
        80
      
      
        orderby
      
       grouped.Average(m =>
      
         m.Score) descending

                                       
      
      
        select
      
      
        new
      
      
        

                                               {

                                                   grouped.Key.Name,

                                                   grouped.Key.Term,

                                                   Scores 
      
      = grouped.Average(m =>
      
         m.Score)

                                               }).ToList();

            
      
      
        foreach
      
       (
      
        var
      
       l 
      
        in
      
      
         AvgScoreGreater80_1)

            {

                Console.WriteLine(
      
      
        "
      
      
        姓名:{0},學期:{1},均分:{2}
      
      
        "
      
      
        , l.Name, l.Term, l.Scores);

            }

            Console.WriteLine(
      
      
        "
      
      
        ---------第二種寫法
      
      
        "
      
      
        );

            
      
      
        /*
      
      
        此寫法看起來較為復雜,第一個Groupby,由于是要對多個字段分組的,因此構建一個匿名對象,

            對這個匿名對象分組,分組得到的其實是一個IEnumberable<IGrouping<匿名類型,StudentScore>>這樣一個類型。

            Where方法接受,和返回的都同樣是IEnumberable<IGrouping<匿名類型,StudentScore>>類型,

            其中Where方法簽名Func委托的類型也就成了Func<IGrouping<匿名類型,StudentScore>,bool>,

            之前說到,IGrouping<out TKey, out TElement>繼承了IEnumerable<TElement>,

            因此這種類型可以有Average,Sum等方法。 
      
      
        */
      
      
        var
      
       AvgScoreGreater80_2 = lst.GroupBy(l => 
      
        new
      
      
         {l.Name, l.Term})

                .Where(m 
      
      => m.Average(x => x.Score) >= 
      
        80
      
      
        )

                .OrderByDescending(l 
      
      => l.Average(x =>
      
         x.Score))

                .Select(l 
      
      => 
      
        new
      
       {l.Key.Name, l.Key.Term, Scores = l.Average(m =>
      
         m.Score)}).ToList();

            
      
      
        foreach
      
       (
      
        var
      
       l 
      
        in
      
      
         AvgScoreGreater80_2)

            {

                Console.WriteLine(
      
      
        "
      
      
        姓名:{0},學期:{1},均分:{2}
      
      
        "
      
      
        , l.Name, l.Term, l.Scores);

            }

  

            Console.ReadKey();

        }
      
    


原文地址: http://hi.baidu.com/tewuapple/item/5e0e5a2862b67a8b9c63d103

?

?

?

?

Linq中GroupBy方法的使用總結


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。。?/p>

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 亚洲欧洲日韩国产aa色大片 | 狼人综合伊人 | 国产黄网永久免费 | 久久艹免费视频 | 奇米影视一区二区三区 | 美女视频很黄很黄又免费的 | 中文字幕在线国产 | 亚洲国产第一区二区香蕉 | 在线成人爽a毛片免费软件 在线成人天天鲁夜啪视频 在线成人亚洲 | 国产伊人影院 | 狠狠色丁香久久婷婷综 | 天天综合天天做 | 久久亚洲一区二区 | 无遮挡无遮挡91桃色在线观看 | 亚洲九九爱 | 最新日本一级中文字幕 | 四月婷婷七月婷婷综合 | 91精品国产综合久久久久久 | 天天干天天拍天天射天天添天天爱 | 四虎影视在线影院www | 国产精品久久久久毛片 | 久久久久久久网站 | 日本特级黄毛片毛片视频 | 亚洲免费美女视频 | 精品久久天干天天天按摩 | 毛片免费看看 | 大尺度福利视频在线观看网址 | 四虎在线精品 | 97av视频在线播放 | 91视频日韩 | 亚洲激情视频网站 | 伊人久久亚洲综合 | 99自拍视频在线观看 | 国产精品好好热在线观看 | 国模和精品嫩模私拍视频 | 在线观看一区二区精品视频 | 一个色的综合 | 国产一区三区二区中文在线 | 成人久久在线 | 国产高清对白国产露脸91 | 国产激情小视频 |