我的代码在这里
VisitorLog log = db.Context.VisitorLogs
.Where(vl=>vl.inDate.Date == DateTime.Now.Date).FirstOrDefault();
显示此错误
The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported
如何仅在LINQ中按日期(skip time)
进行比较?
正确答案
var currentDate = DateTime.Now;
VisitorLog log = db.Context.VisitorLogs
.Where(vl => EntityFunctions.DiffDays(vl.inDate, currentDate) == 0).FirstOrDefault();
谢谢你回答。
答案 0 :(得分:2)
改为执行此操作,
var currentDate = DateTime.Now.Date;
VisitorLog log = db.Context.VisitorLogs
.Where(vl=>vl.inDate.Date == currentDate).FirstOrDefault();
但是如果vl.inDate.Date包含时间,这可能会很棘手,但它不会出错,只是可能没有匹配
编辑:
鉴于这不起作用,你可以尝试这项工作吗
var currentDate = DateTime.Now.Date;
var timeIn24HrsTime = DateTime.Now.AddDays(1).Date;
VisitorLog log = db.Context.VisitorLogs
.Where(vl=>vl.inDate >= currentDate && vl.inDate < timeIn24HrsTime).FirstOrDefault();
答案 1 :(得分:2)
答案 2 :(得分:2)
你也可以试试这个:
context.Products.Where(x=> (
EntityFunctions.TruncateTime(x.CreatedDate) >=
EntityFunctions.TruncateTime(startdate))
&& (EntityFunctions.TruncateTime(x.CreatedDate) <=
EntityFunctions.TruncateTime(enddate)))
根据您的要求 你应该尝试:
var currentDate = DateTime.Now;
VisitorLog log = db.Context.VisitorLogs.Where(vl =>
EntityFunctions.DiffDays(EntityFunctions.TruncateTime(vl.inDate),
EntityFunctions.TruncateTime(currentDate)) == 0).FirstOrDefault();
答案 3 :(得分:1)
尝试
vl.inDate.Date.Equals(DateTime.Now.Date)