My task is to return the number of cars where the age is more than three years
public IActionResult Report()
{
var result = _context.Cars.FromSqlRaw("SELECT COUNT(*) from Car where (DATEDIFF(DAY, ReleaseDate, GetDate()) / 365.25) > 3");
ViewBag.result = result;
return View();
}
After spending time googling and studying Microsoft documentation I figured out that FromSqlRaw will work but in web form it returns either -1 or Microsoft.EntityFrameworkCore.Query.Internal.Entityqueryable<T>.
A direct query to the database returns the correct value which is 1 car.
Adding any parameters after FromSqlRaw returns an exception.
I just don't get or getting ReleaseDate and then formatting and dividing it is too much?