如何在where子句中比较agedate与agedate的比较?像这段代码:
myRepository.Where(x =>
fromAge <= x.BirthDate.Age && x.BirthDate.Age <= toAge)
.Select(x).toList();
数据类型:
fromAge,toAge:int
x.BirthDate:DateTime
答案 0 :(得分:1)
搜索后,我最终得到了这个解决方案:
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();
答案 1 :(得分:0)
我认为x.BirthDate
是一个DateTime。
如果DateTime代表他们的D.O.B,您可以获得一个人的年龄。像这样:
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)
,否则您将获得例外。
答案 2 :(得分:0)
以下列方式构造WHERE子句
myRepository.Where(x =>
fromAge <= (Datetime.Now.Year - x.BirthDate.Year) && (Datetime.Now.Year - x.BirthDate.Year) <= toAge)
.Select(x).toList();
答案 3 :(得分:0)
假设fromAge和toAge是岁月:
from sklearn.model_selection import cross_val_predict
from sklearn.metrics import confusion_matrix
y_pred = cross_val_predict(clf, x, y, cv=10)
conf_mat = confusion_matrix(y, y_pred)
答案 4 :(得分:0)
我认为你必须将它添加到您的BirthDate类:
[NotMapped]
public int Age {
get {
DateTime now = DateTime.Now;
int age = now.Year - BirthDate.Year;
if(now < BirthDate.AddYears(age)) age--;
return age;
}
}
这是让你的年龄属性从出生日期自动计算