linq sum where

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

linq sum where 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;

 

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

linq sum where View Code
 int totalScore = 0;

             foreach (StuItem sientity in stEntityList)

            {

                 if (sientity.SScore != null)

                {

                    totalScore += sientity.SScore.Value;

                }

                

             }

你可能感兴趣的:(where)