【发布时间】:2017-05-24 00:06:08
【问题描述】:
我的成员表有一个名为 Birthdate 的可为空的 DateTime 字段。我需要获取在接下来的 n 天内过生日的成员。我尝试了几种方法,但都没有奏效。
1
DateTime startDate = DateTime.Now;
DateTime endDate = DateTime.Now.AddDays(n);
GetAll().Where(
x => x.BirthDate != null
&& new DateTime(startDate.Year, x.BirthDate.Value.Month, x.BirthDate.Value.Day, 1, 1, 1) >= startDate
&& new DateTime(endDate.Year, x.BirthDate.Value.Month, x.BirthDate.Value.Day, 1, 1, 1) <= endDate
);
不能将 x.BirthDate 传递给 DateTime 构造函数
2
GetAll().OrderByDescending(m => m.CreateDate)
.Where(x => x.BirthDate.HasValue
&& (endDate - x.BirthDate.Value).Days <= n)
引发识别错误。
您知道任何可行且简单的方法吗?
【问题讨论】:
-
您是否尝试过在开始过滤之前先查看
GetAll()返回的内容?
标签: c# nhibernate date