0

I want to compare two dates but i don't know what is the problem ?? someone help me

enter image description here

  [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);
    }
1
  • do you want to check whether date is exist or not in table? Commented May 21, 2020 at 9:54

3 Answers 3

1

Why wouldn't you just use the following?

db.Events.Any(p => p.StartDate == startdate);

It can be used with or without .ToList().

Sign up to request clarification or add additional context in comments.

Comments

0

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.

like:

bool isValid = DateTime.Compare(p.StartDate, startDate) == 0;

1 Comment

i want to check if startdate in my datatable is exist or not
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.