【隨記】關於List集合用Linq GroupBy分組過后的遍歷小記


List<LeaderKaoQin> lstLeader = new List<LeaderKaoQin>();//一個List集合
IGrouping<string, LeaderKaoQin> IGroups = null;//由於 group 查詢產生的 IGrouping<TKey,TElement> 對象實質上是列表的列表,所以需遍歷2次

List<IGrouping<string, LeaderKaoQin>>    deptLeaders = (from m in lstLeader group m by m.DepName into G orderby G.Key select G).ToList();

            //此處需要遍歷2次

    double deptJB = 0;
            double deptZC = 0;
            for (int i = 0; i < deptLeaders.Count; i++)
            {
                deptJB = 0;
                deptZC = 0;
                IGroups = deptLeaders[i];
                sbDept.Append(string.Format("'{0}',", IGroups.Key));
      
                foreach (var item in IGroups)
                {
                    deptJB += item.Overtime;
                    deptZC += item.Normal;
                }
                sbDeptJB.Append(string.Format("{0},", deptJB.ToString("f2")));//保留2位小數
                sbDeptZC.Append(string.Format("{0},", deptZC.ToString("f2")));
            }

參考資料:

https://msdn.microsoft.com/zh-cn/library/bb384063.aspx


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM