I am calling the below code in my api from a mobile device and from a Rest client:
[HttpGet]
[Route("api/mobile/GetUsersList")]
public IHttpActionResult GetUsersList()
{
var users = from c in db.Users
select new { c.id, c.user_name, c.reports_to };
return Ok(users.ToList());
}
I get an error of:
The cast to value type 'System.Int32' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type
I am not using a where so I'm not sure why this simple query would throw this error.
I am testing the Http GET to this function using Advanced Rest Client.
I see in other SO posts that this error gets thrown when a where parameter is null.
Ryan
Try to specify name directly with cast
var users = from c in db.Users
select new { id= (int?)c.id, user_name = c.user_name, reports_to = c.reports_to };
or
id = c.id ?? 0