我的成员表有一个名为Birthdate的可为空的DateTime字段。我需要在接下来的n天内找到生日的成员。我尝试了一些方法,但没有一种方法有效。
1
WITH MYVALUES AS (
SELECT 1 MYVALUE FROM DUAL
UNION ALL SELECT 3 FROM DUAL
UNION ALL SELECT 2 FROM DUAL
UNION ALL SELECT 5 FROM DUAL
UNION ALL SELECT 3 FROM DUAL
UNION ALL SELECT 2 FROM DUAL
UNION ALL SELECT 4 FROM DUAL
)
SELECT
MYVALUE,
SUM (MYVALUE) OVER (PARTITION BY NULL ORDER BY NULL ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING) S
FROM MYVALUES
无法将x.BirthDate传递给DateTime构造函数
2
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
);
抛出识别错误。
你知道有什么工作和简单的方法吗?
答案 0 :(得分:0)
linq-to-nhibernate支持DateTime
和DateTimeOffset
上的一系列属性。您可以看到列表here。 reference documentation有一天也会有专门的Linq部分。 (已经提交,尚未发布。)
因此,猜测您的GetAll
会产生IQueryable
,您可以这样编写查询:
var startDate = DateTime.Now;
var endDate = DateTime.Now.AddDays(n);
if (startDate.Year == endDate.Year)
{
// Simple case, just compare months and days.
GetAll().Where(
x => x.BirthDate.Value.Month >= startDate.Month &&
x.BirthDate.Value.Day >= startDate.Day &&
x.BirthDate.Value.Month <= endDate.Month &&
x.BirthDate.Value.Day <= endDate.Day);
}
else
{
// Range spanning two distinct years, so matching dates
// are either lower months and days than endDate, OR
// greater months and days than startDate. (Cannot have both.)
GetAll().Where(
x => x.BirthDate.Value.Month >= startDate.Month &&
x.BirthDate.Value.Day >= startDate.Day ||
x.BirthDate.Value.Month <= endDate.Month &&
x.BirthDate.Value.Day <= endDate.Day);
}
我已经删除了对HasValue
的检查:如果查询被转换为SQL,则在这种情况下SQL不需要它。
顺便说一句,它应该与queryover一起使用,我相信它支持相同的DateTime
属性。