I’ve written a LINQ query that should return an int value. but I’m not able to convert this value to int and an exception occured:
Unable to cast object of type ‘QuickRoutes.DAL.RouteLinq’ to type ‘System.IConvertible’.
this is my LINQ :
var res = (from p in aspdb.RouteLinqs
orderby p.RouteId descending
select p).Take(1);
and the exception occures here:
route.Id =Convert.ToInt32(res.FirstOrDefault());
how can I solve this problem?
that is because
res.FirstOrDefault()returns aRouteLinqstype object, because you are usingselect p, you may select a field usingselect p.FieldName, where FieldName is the property you require for conversion, which is probablyRouteIdYou may want to query the particular field in your linq query.
Or with your current query, you may do: