[英]Comparing two dates in asp.net mvc
I want to compare two dates but i don't know what is the problem??我想比较两个日期,但我不知道是什么问题?? someone help me
谁来帮帮我
[HttpPost]
public JsonResult Checkdate(DateTime startdate)
{
Entities1 db = new Entities1();
bool isValid = !db.Events.ToList().Exists(p => p.StartDate.Equals(startdate, StringComparison.CurrentCultureIgnoreCase));
return Json(isValid);
}
Why wouldn't you just use the following?您为什么不只使用以下内容?
db.Events.Any(p => p.StartDate == startdate);
It can be used with or without .ToList()
.它可以与或不与
.ToList()
一起使用。
I do not understand what you are trying to achieve, but for date time comparisons, it is wiser to use Datetime.Compare(date1, date2) method.我不明白您要达到什么目的,但是对于日期时间比较,使用 Datetime.Compare(date1, date2) 方法更明智。
like:喜欢:
bool isValid = DateTime.Compare(p.StartDate, startDate) == 0;
The other answers should work but just to include this try looking for:其他答案应该有效,但只是为了包括这个尝试寻找:
bool exists = false;
var event = db.Events.Where(p => p.StartDate == startdate);
if (event != null){
exists = true;
}
this way not only do you know if it's true but you have a place to hold it and see it later这样你不仅知道它是否是真的,而且你有一个地方可以拿着它,以后再看
you can actually return it in the Json if you'd like如果您愿意,您实际上可以在 Json 中返回它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.