linq sum where

本人第一次使用linq,现在需要的功能是,有个List<T> ,T里有一列SScore,我想取得SScore不等于null的所有值的总和。写法如下 

View Code
 IList<StuItem> stEntityList = new List<StuItem>();
            StuItem stBll = new StuItem();
            stEntityList = stBll.GetModelList("");
 
            int totalScore = (from s in stEntityList where s.SScore != null select s.SScore).Sum().Value;

 其实功能与下面的代码效果一致:  

View Code
 int totalScore = 0;
             foreach (StuItem sientity in stEntityList)
            {
                 if (sientity.SScore != null)
                {
                    totalScore += sientity.SScore.Value;
                }
                
             }
原文地址:https://www.cnblogs.com/puzi0315/p/2483612.html