public JsonResult GetItemCode(string Code)
{
double Category = objFatchXEntities.ItemCardUAE.Single(s => s.Code == Code).Price;
return Json(Category, JsonRequestBehavior.AllowGet);
}
Error CS0266 Cannot implicitly convert type 'double?' to 'double'. An explicit conversion exists (are you missing a cast?)
CodePudding user response:
It is because your query returns double?. You can change type for Category but better to use var
var category = objFatchXEntities.....
return Json(category, JsonRequestBehavior.AllowGet);
but it is always better idea to check for null
var category = objFatchXEntities.ItemCardUAE.FirstOrDefault(s => s.Code == Code);
if (category == null || category.Price==null) return null;
return Json(category.Price, JsonRequestBehavior.AllowGet);
CodePudding user response:
You declared a double variable, it expects a double not a nullable double. You could either get the value of the double?
so you have matching types like this
double Category = objFatchXEntities.ItemCardUAE.Single(s => s.Code == Code).Price.Value;
But the problem with this is that you get a NullReference error if price is null at any point. You could just check if Price has a value before doing anything like this
if (objFatchXEntities.ItemCardUAE.Single(s => s.Code == Code).Price.HasValue)
{
... do something
}