linq时间筛选以及list时间筛选
Linq的时间筛选
//写法一:正常 IEnumerable<x> xList = dbContext.xs.OrderByDescending(u => u.CreateTime).Where(u => EntityFunctions.DiffDays(u.CreateTime, DateTime.Now) == 0); //写法二:错误 IEnumerable<x> xList = dbContext.xs.OrderByDescending(u => u.CreateTime); xList = xList.Where(u => EntityFunctions.DiffDays(u.CreateTime, DateTime.Now) == 0); 写法二中,在使用EntityFunctions的时候,xList对象已经是Ienumerable的类型了,是无法使用linq自带的EntityFunctions方法的,所以会报错,具体意思就是不识别这个方法
List的时间筛选,这里有一点是DateTime中的Date是表示日期,例如今天2017/10/17 9:45.0,Date处理后的结果就是 2017/10/17 0:00:0
#region List获取 List<TestObj> list = new List<TestObj>(); for (int i = 0; i < 4; i++) { TestObj temp = new TestObj() { ID = i, CreateTime = DateTime.Now.AddYears(-i).AddDays(i) }; list.Add(temp); } Console.WriteLine( SerializationHelper.JsonSerialize(list.Where(u => DateTime.Compare(u.CreateTime.Date, DateTime.Now.AddYears(-1).AddDays(1).Date) == 0).ToList())); Console.WriteLine( DateTime.Now.Date); #endregion
fighting