private void BtnAddActivityClick(object sender, RoutedEventArgs e) { ActivityLogService als=new ActivityLogService(); var item = this.CbActivityList.SelectedItem as Activity; if (item != null) { ActivityLog log=new ActivityLog(); if(!dpDate.SelectedDate.HasValue) { MessageBox.Show("请选择日期!"); return; } log.Date = dpDate.SelectedDate.Value; var aa = als.GetAll().Where(a => a.ActivityId == item.Id) .OrderByDescending(a => a.Date) .FirstOrDefault(); if(aa==null) { log.Distance = 0; log.LastDate = DateTime.Now; } else { log.Distance = (DateTime.Now-aa.Date).Days; log.LastDate = aa.Date; } log.ActivityId = item.Id; new ActivityLogService().Add(log); } MessageBox.Show("添加成功!"); }