[Lambda]Group By

 Group By

單一欄位與多個欄位

 將成績單裡的分數Group By起來,計算相同分數的數量。

GroupBy單一欄位:

var transcriptListGroupByScore = transcriptList.GroupBy(c => c.score)
	.Select(c => new
	{
		score = c.Key,
		scoreCount = c.Count()
	}).OrderByDescending(order => order.score).ToList();

GroupBy完後,使用降冪排序(OrderByDescending),從分數大排至小,印出來後,呈現如下

GroupBy多個欄位:

使用new,選擇要GroupBy的欄位,再使用Key選出要顯示的欄位。

var transcriptListGroupByScore = transcriptList.GroupBy(c => new { c.Class, c.score })
    .Select(c => new
    {
        Class = c.Key.Class,
        score = c.Key.score,
        scoreCount = c.Count()
    }).OrderBy(order => order.Class).ThenByDescending(order => order.score).ToList();

之後印出來後,會呈現下方的樣子

 

 

創用 CC 授權條款
本著作由Chenting Weng製作,以創用CC 姓名標示 4.0 國際 授權條款釋出。
This work by Chenting Weng is licensed under a Creative Commons Attribution 4.0 International License.
Based on a work at https://dotblogs.com.tw/chentingw.

部分文章內容會引用到其他網站的簡介或圖片,若有侵犯到您的著作權,請留言告知,本人會儘快移除。
免責聲明:文章屬個人記事使用,僅供參考,若引用文章造成一切損失,本人不承擔任何責任。如有錯誤,歡迎留言告知。

Part of the content of the article will refer to the profile or picture of other websites.
If there is any infringement of your copyright, please leave a message and let me remove it as soon as possible.
Disclaimer:The article is for personal use and is for reference only. I will not bear any responsibility for any loss caused by quoting the article. If there is an error, please leave a message to inform.