本文介绍了如何使用Linq从列表中获取比今天更大和更小的日期数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
此查询可以减少吗?
Can this query reduce ?
public class Bucket
{
public string Name { get; set; }
public double Value { get; set; }
}
List<Service_Result> FollowUpDateresult = InfoDB.Where(x => x.FollowUPDate != null).ToList();
int FollowUpDateLesser = FollowUpDateresult.Where(x => x.FollowUPDate >= DateTime.Now).ToList().Count();
int FollowUpDateGreater = FollowUpDateresult.Where(x => x.FollowUPDate <= DateTime.Now).ToList().Count();
List<Bucket> FinalDateresult = new List<Bucket>();
FinalDateresult.Add( new Bucket { Name = "Future Date's", Value = FollowUpDateGreater });
FinalDateresult.Add(new Bucket { Name = "Past Date''s", Value = FollowUpDateLesser });
推荐答案
List<DateTime?> inputs = new List<DateTime?>();
inputs.Add(new DateTime(1959, 2, 24));
inputs.Add(new DateTime(2016, 2, 24));
inputs.Add(new DateTime(2017, 2, 24));
inputs.Add(null);
inputs.Add(null);
inputs.Add(null);
DateTime now = DateTime.Now;
List<Bucket> DateResult = inputs.GroupBy(d => d == null ? "NullDates" : ((DateTime)d - now).TotalSeconds >= 0 ? "GreaterDate (Count)" : "LesserDate (Count)").Select(g => new Bucket() { Name = g.Key, Value = g.Count() }).ToList();
这篇关于如何使用Linq从列表中获取比今天更大和更小的日期数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!