计算列表中最近7天的分钟数

时间:2015-09-19 05:36:04

标签: c# linq windows-phone-8.1 windows-phone

我正在编写 Windows Phone 8.1应用程序(WINPRT XAML)

   public class AchievementsRecordType
    {
        public DateTime Date { get; set; }
        public int Minutes { get; set; }
        public int WorkOutID { get; set; }

    }

我制作了 AchievementsRecordType 类型的列表 AchievementsRecordList ,并在其中添加了一些日期信息:

示例:

AchievementsRecordList.Add(new AchievementsRecordType { Date = DateTime.Today, Minutes = 7, WorkOutID = int.Parse(SelectedWorkoutID) });
该列表的

输出例如:

  

18-09-2015,7,510   20-09-2015,7,120

     

21-09-2015,7,433   21-09-2015,7,437   21-09-2015,7,438

     

24-09-2015,7,120   24-09-2015,7,121

     

25-09-2015,7,340   25-09-2015,7,341   25-09-2015,7,342

现在今天是26-09-2015,如何计算过去7天的总分数

2 个答案:

答案 0 :(得分:1)

 AchievementsRecordList
.Where(x=>x.Date >= DateTime.Today.AddDays(-7)&& x.Date < DateTime.Today)
.Sum(y=>y.Minutes);

答案 1 :(得分:1)

lst.Where(x=>x.Date >= DateTime.Today.AddDays(-7) && x.Date <=DateTime.Today).Sum(x=>x.Minutes)