Joe walter Joe walter - 4 months ago 8
C# Question

some rows are missing on results of my query

I want to get report between 2 dates with PersianCalendar and I've already converted the dates. It does not show me the data about last day
This is my code:

public ActionResult FilterDate(DateFormViewModel model)
{
PersianCalendar pc = new PersianCalendar();
var dateFrom = pc.ToDateTime(model.FromYear, model.FromMonth, model.FromDay, 0, 0, 0, 0);
var dateTo = pc.ToDateTime(model.ToYear, model.ToMonth, model.ToDay, 0, 0, 0, 0);
var filter = db.Parts.Where(s => s.CreateDateTime >= dateFrom && s.CreateDateTime <= dateTo).ToList();
return View("Index", filter);
}

Answer

You are missing data on the last day, right? (15 mordad) The reason is that you are looking to the same day. having <= in the query does not help you. for instance 1395/5/15 01:00:00 is greater than 1395/5/15 (your dateTo) and therefore it will not show up in the results

you have to check it with next day

  var filter = db.Parts.Where(s => s.CreateDateTime >= dateFrom && s.CreateDateTime < dateTo.AddDay(1)).ToList();