在 where 子句中比较年龄和生日
compare age with birthdate in where clause
如何在where子句中比较年龄和生日?像这样的代码:
myRepository.Where(x =>
fromAge <= x.BirthDate.Age && x.BirthDate.Age <= toAge)
.Select(x).toList();
数据类型:
从年龄,到年龄:整数
x.BirthDate: 日期时间
我假设 x.BirthDate
是一个 DateTime。
您可以根据代表 D.O.B 的 DateTime 获取一个人的年龄。像这样:
double age = Math.Floor(DateTime.Now.Subtract(BirthDate).TotalDays / 365.25m);
你可以把它变成一个扩展方法:
public static int Age(this DateTime birthDate)
{
return (int)Math.Floor(DateTime.Now.Subtract(birthDate).TotalDays / 365.25m);
}
然后你可以做:
myRepository.Where(x => fromAge <= x.BirthDate.Age() && x.BirthDate.Age() <= toAge).ToList();
但是请注意,如果 myRepository
是 EntityFramework DbSet
,这将不起作用。你必须做 myRepository.AsEnumerable().Where(// etc)
,否则你会得到一个例外。
按以下方式构造一个WHERE子句
myRepository.Where(x =>
fromAge <= (Datetime.Now.Year - x.BirthDate.Year) && (Datetime.Now.Year - x.BirthDate.Year) <= toAge)
.Select(x).toList();
假设 fromAge 和 toAge 是年:
myRepository.Where(x => DateTime.Now.AddYears(-fromAge) <= x.BirthDate && x.BirthDate <= DateTime.Now.AddYears(-toAge)).ToList();
我认为你必须将此添加到你的出生日期 class:
[NotMapped]
public int Age {
get {
DateTime now = DateTime.Now;
int age = now.Year - BirthDate.Year;
if(now < BirthDate.AddYears(age)) age--;
return age;
}
}
这是让你的年龄属性从生日
自动计算
经过搜索,我得到了这个解决方案:
toDate = DateTime.Now.AddYears(-fromAge).Date.AddYears(1).AddDays(-1);
fromDate = DateTime.Now.AddYears(-toAge).Date.AddYears(-1).AddDays(+1);
考虑将 "fromAge" 更改为 "toDate",将 "toAge" 更改为 "fromDate"。所以:
myRepository.Where(x => fromDate <= x.BirthDate && x.BirthDate <= toDate)
.Select(x).toList();
如何在where子句中比较年龄和生日?像这样的代码:
myRepository.Where(x =>
fromAge <= x.BirthDate.Age && x.BirthDate.Age <= toAge)
.Select(x).toList();
数据类型:
从年龄,到年龄:整数
x.BirthDate: 日期时间
我假设 x.BirthDate
是一个 DateTime。
您可以根据代表 D.O.B 的 DateTime 获取一个人的年龄。像这样:
double age = Math.Floor(DateTime.Now.Subtract(BirthDate).TotalDays / 365.25m);
你可以把它变成一个扩展方法:
public static int Age(this DateTime birthDate)
{
return (int)Math.Floor(DateTime.Now.Subtract(birthDate).TotalDays / 365.25m);
}
然后你可以做:
myRepository.Where(x => fromAge <= x.BirthDate.Age() && x.BirthDate.Age() <= toAge).ToList();
但是请注意,如果 myRepository
是 EntityFramework DbSet
,这将不起作用。你必须做 myRepository.AsEnumerable().Where(// etc)
,否则你会得到一个例外。
按以下方式构造一个WHERE子句
myRepository.Where(x =>
fromAge <= (Datetime.Now.Year - x.BirthDate.Year) && (Datetime.Now.Year - x.BirthDate.Year) <= toAge)
.Select(x).toList();
假设 fromAge 和 toAge 是年:
myRepository.Where(x => DateTime.Now.AddYears(-fromAge) <= x.BirthDate && x.BirthDate <= DateTime.Now.AddYears(-toAge)).ToList();
我认为你必须将此添加到你的出生日期 class:
[NotMapped]
public int Age {
get {
DateTime now = DateTime.Now;
int age = now.Year - BirthDate.Year;
if(now < BirthDate.AddYears(age)) age--;
return age;
}
}
这是让你的年龄属性从生日
自动计算经过搜索,我得到了这个解决方案:
toDate = DateTime.Now.AddYears(-fromAge).Date.AddYears(1).AddDays(-1);
fromDate = DateTime.Now.AddYears(-toAge).Date.AddYears(-1).AddDays(+1);
考虑将 "fromAge" 更改为 "toDate",将 "toAge" 更改为 "fromDate"。所以:
myRepository.Where(x => fromDate <= x.BirthDate && x.BirthDate <= toDate)
.Select(x).toList();