I have following class:
public class Top10Date
{
[Key]
public int DateId { get; set; }
public DateTime Date { get; set; }
}
Which is associated with Top10Date table in database. I am trying to do a comparison and assign values to latestDate in controller class:
public ActionResult Index(string date, DateTime? latestDate)
{
var topDate = db.Top10Dates;
if (!latestDate.HasValue)
latestDate = (DateTime?)(topDate.OrderByDescending(d => d.Date).FirstOrDefault());
if (date.Value == "PreviousDate")
{
latestDate = (DateTime?)(topDate.Where(d => d.Date < latestDate.Date).OrderByDescending(d => d.Date).FirstOrDefault());
}
if (date.Value == "NextDate")
{
latestDate = (DateTime?)(topDate.Where(d => d.Date > latestDate.Date).OrderBy(d => d.Date).FirstOrDefault());
}
....
ViewBag.latestDate = latestDate
return View();
}
But it is giving me all sort of errors. I have tried other things too but none are working:
latestDate = topDate.OrderByDescending(d => d.Date).FirstOrDefault();
latestDate.Value = topDate.OrderByDescending(d => d.Date).FirstOrDefault();
latestDate = topDate.Where(d => d.Date > latestDate.Value.Date).OrderBy(d => d.Date).FirstOrDefault();
Also how can i get DateId from the Database? I tried this it is now working.
var dateId = (DateTime?)top10Date.Where(d => d.Date == latestDate.Value.Date).Select(x => x.DateId).FirstOrDefault();