I want to pass the result of an Entity query to a datatable, but I haven't been able to because it gives me an error the way I'm doing it using CopyToDataTable()
'Cannot convert an object of type 'System.Data.Entity.Infrastructure.DbQuery
1[<>f__AnonymousType4
1[System.Int32]]' to type 'System.Collections.Generic.IEnumerable`1[System.Data.DataRow]'.'
the code I use is:
public DataTable QueryResultados()
{
string situacion;
if (RadCmbAccion.SelectedValue == "1")
{
situacion = "4";
}
else
{
situacion = "3";
}
string valoresCtas = regresa_lista().ToString();
CatalogoContainer entidad = new CatalogoContainer();
IEnumerable<DataRow> query = (IEnumerable<DataRow>)(from c in entidad.CUENTA
join ct in entidad.CUENTA_TIPO
on c.id_CUENTA_TIPO equals ct.id_CUENTA_TIPO
join ffp in entidad.FORMA_PAGO
on c.id_FORMA_PAGO equals ffp.id_FORMA_PAGO
join cs in entidad.CUENTA_SITUACION
on c.id_CUENTA_SITUACION equals cs.id_CUENTA_SITUACION
where c.id_CUENTA_SITUACION.Equals(situacion) && valoresCtas.Contains(c.id_CUENTA.ToString())
select new
{
c.id_CUENTA
});
DataTable boundTable = (query.CopyToDataTable());
return boundTable;
}
Does anyone know how to correct this error?
The problem is probably of the anonymous type. Try this:
I have already solved my question by removing
adding a class ChangeStatus() with the properties that I want to be shown in the query, in this case with id_ACCOUNT of type int and doing a json deserealization of the result of my query as follows