统计每个季度的数据

 private object GetSeasonDataByYear(DateTime thisTime,
            List<DailyDialysisWayStatistic> baseStatisticsList, List<string> dialysisWayList)
        {
            //源格式:透析方式-季度数组
            List<Tuple<string, object>> tupleList = new List<Tuple<string, object>>();

            var seasonGroups = new[] { "第一季度", "第二季度", "第三季度", "第四季度" };
            dialysisWayList.ForEach(y =>
            {
                int[] countGroups = new int[4];
                for (int i = 1; i <= seasonGroups.Length; i++)
                {
                    DateTime[] seasonDate = GetSeasonDate(thisTime.Year, i);
                    var count = baseStatisticsList
                        .Where(z => z.DialysisWay == y && z.DialysisDate >= seasonDate.First() &&
                                    z.DialysisDate <= seasonDate.Last())
                        .Sum(x => x.Frequency);
                    countGroups[i - 1] = count;
                }
                tupleList.Add(new Tuple<string, object>(y, countGroups));
            });

            return new { seasonGroups, tupleList };
        }



 /// <summary>
        /// 获取输入年-季度的第一天与最后一天
        /// </summary>
        /// <param name="thisYear">当前年</param>
        /// <param name="seasonNum">季度序号</param>
        /// <returns></returns>
        private DateTime[] GetSeasonDate(int thisYear, int seasonNum = 1)
        {
            var firstDate = new DateTime(thisYear, (seasonNum - 1) * 3 + 1, 1);

            //季度最后一天默认30号,1-4季度为31号.
            var day = seasonNum == 1 || seasonNum == 4 ? 31 : 30;
            var lastDate = new DateTime(thisYear, (seasonNum - 1) * 3 + 3, day);

            return new[] { firstDate, lastDate };
        }

 

posted @ 2018-07-23 15:54  小魔女srn  阅读(368)  评论(0编辑  收藏  举报