I am working with Entity Framework with mvc and on the client side I have implemented a Select2 for this I have created the following model:
public class Select2
{
public int id { get; set; }
public string text { get; set; }
public Object[] data { get; set; }
}
The id and text properties are mandatory for a select2, the data property was created to save all the data that I extract in the query since previously it only returned the id and the name that were in the properties id and text then I want add an array of objects to save all the other columns of my query.
I use this code:
[HttpGet]
public async Task<ActionResult> GetMesa(string q)
{
try
{
if (String.IsNullOrEmpty(q) || String.IsNullOrWhiteSpace(q))
{
return Json(new { respuesta="El filtro no puede ser nulo o un espacio en blanco /n" }, JsonRequestBehavior.AllowGet);
}
else
{
var items = await db.Mesa.Where(x => x.NombreMesa.ToLower().StartsWith(q.ToLower()))
.Select(x => new Select2()
{
id = x.MesaID,
text = x.NombreMesa,
data = new object[] { x.MesaID, x.NumeroMesa, x.NombreMesa, x.CapacidadMesa, x.AccionMesa, x.PisoMesa }
})
.ToListAsync();
return Json(new { respuesta = "OK", items = items },JsonRequestBehavior.AllowGet);
}
}
catch (Exception e)
{
return Json(new {respuesta = "MesasController => GetMesa \n" + e.Message + e.InnerException}, JsonRequestBehavior.AllowGet);
}
}
The following error jumps me when I assign the value to items :
Can not convert the type 'System.Int32' to the type 'System.Object'. LINQ to Entities only supports the conversion of enumeration types or EDM primitives.
How can I solve it? Or should I just use another form?