estoy tratando de ingresar una lista de objetos en un datatable en ASP.NET Core ,estoy uniendo dos tablas , las tablas son usuarios y rol , las cuales estoy sacando campos de ambas tablas , utilizo linq para unir los campos , me da una lista de objetos y esos objetos quiero que se agreguen en un datatable y poder mostrar la lista de objetos, al enviar en postman me da error quiero que me iste los usuarios que tienen el mismo código sap , pero también que me muestre el nombre del rol y ese campo se encuentra en la tabla rol , me debolverá varios usuarios
este es mi repository
public DataTable buscarUsuario(string cod_sap)
{
DataTable dt = new DataTable("listaUsuariosXrol");
DataColumn usu_nom_ape = new DataColumn("nombre");
DataColumn usu_usu = new DataColumn("usuario");
DataColumn usu_cla = new DataColumn("clave");
DataColumn usu_cor = new DataColumn("correo");
DataColumn usu_cod_sap = new DataColumn("cod_sap");
DataColumn usu_est = new DataColumn("estado");
DataColumn usu_cod_rol = new DataColumn("rol");
DataColumn rol_nom = new DataColumn("nombre_rol");
dt.Columns.Add(usu_nom_ape);
dt.Columns.Add(usu_usu);
dt.Columns.Add(usu_cla);
dt.Columns.Add(usu_cor);
dt.Columns.Add(usu_cod_sap);
dt.Columns.Add(usu_est);
dt.Columns.Add(usu_cod_rol);
dt.Columns.Add(rol_nom);
var usuario = (from usu in _quimpacContext.Usuarios
join r in _quimpacContext.Rol
on usu.usu_cod_rol equals r.rol_cod
where usu.usu_cod_sap.Equals(cod_sap)
select new
{
usu.usu_nom_ape,
usu.usu_usu,
usu.usu_cla,
usu.usu_cor,
usu.usu_cod_sap,
usu.usu_est,
usu.usu_cod_rol,
r.rol_nom
}).ToList();
foreach (var item in usuario)
{
//creas una nueva row
DataRow row = dt.NewRow();
//asignas el dato a cada columna de la row
row["nombre"] = item.usu_nom_ape;
row["usuario"] = item.usu_usu;
row["clave"] = item.usu_cla;
row["correo"] = item.usu_cor;
row["cod_sap"] = item.usu_cod_sap;
row["estado"] = item.usu_est;
row["rol"] = item.usu_cod_rol;
row["nombre_rol"] = item.rol_nom;
dt.Rows.Add(row);
}
return dt;
}
el erro que me da es el siguiente
System.NotSupportedException: Serialization and deserialization of 'System.Type' instances are not supported and should be avoided since they can lead to security issues. Path: $.Columns.DataType.
---> System.NotSupportedException: Serialization and deserialization of 'System.Type' instances are not supported and should be avoided since they can lead to security issues.
at System.Text.Json.Serialization.Converters.TypeConverter.Write(Utf8JsonWriter writer, Type value, JsonSerializerOptions options)
at System.Text.Json.Serialization.JsonConverter`1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
at System.Text.Json.JsonPropertyInfo`1.GetMemberAndWriteJson(Object obj, WriteStack& state, Utf8JsonWriter writer)
at System.Text.Json.Serialization.Converters.ObjectDefaultConverter`1.OnTryWrite(Utf8JsonWriter writer, T value, JsonSerializerOptions options, WriteStack& state)
at System.Text.Json.Serialization.JsonConverter`1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
at System.Text.Json.Serialization.JsonConverter`1.TryWriteAsObject(Utf8JsonWriter writer, Object value, JsonSerializerOptions options, WriteStack& state)
at System.Text.Json.Serialization.JsonConverter`1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
at System.Text.Json.Serialization.Converters.IEnumerableConverter`1.OnWriteResume(Utf8JsonWriter writer, TCollection value, JsonSerializerOptions options, WriteStack& state)
at System.Text.Json.Serialization.Converters.IEnumerableDefaultConverter`2.OnTryWrite(Utf8JsonWriter writer, TCollection value, JsonSerializerOptions options, WriteStack& state)
at System.Text.Json.Serialization.JsonConverter`1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
at System.Text.Json.JsonPropertyInfo`1.GetMemberAndWriteJson(Object obj, WriteStack& state, Utf8JsonWriter writer)
at System.Text.Json.Serialization.Converters.ObjectDefaultConverter`1.OnTryWrite(Utf8JsonWriter writer, T value, JsonSerializerOptions options, WriteStack& state)
at System.Text.Json.Serialization.JsonConverter`1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
at System.Text.Json.Serialization.JsonConverter`1.WriteCore(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
--- End of inner exception stack trace ---
at System.Text.Json.ThrowHelper.ThrowNotSupportedException(WriteStack& state, NotSupportedException ex)
at System.Text.Json.Serialization.JsonConverter`1.WriteCore(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
at System.Text.Json.Serialization.JsonConverter`1.WriteCoreAsObject(Utf8JsonWriter writer, Object value, JsonSerializerOptions options, WriteStack& state)
at System.Text.Json.JsonSerializer.WriteCore[TValue](JsonConverter jsonConverter, Utf8JsonWriter writer, TValue& value, JsonSerializerOptions options, WriteStack& state)
at System.Text.Json.JsonSerializer.WriteAsyncCore[TValue](Stream utf8Json, TValue value, Type inputType, JsonSerializerOptions options, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Mvc.Formatters.SystemTextJsonOutputFormatter.WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeResultAsync>g__Logged|21_0(ResourceInvoker invoker, IActionResult result)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResultFilterAsync>g__Awaited|29_0[TFilter,TFilterAsync](ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters()
--- End of stack trace from previous location ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker)
at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
quiero que me retorne lo siguiente
[
{
"usu_nom_ape": "Christian Espinoza ",
"usu_usu": "cespinoza",
"usu_cla": "123456",
"usu_cor": "[email protected]",
"usu_cod_sap": "0000105680",
"usu_est": "1",
"usu_cod_rol": 1,
"usu_cod_cli": 1,
"rol_nom": "Administrador"
},
{
"usu_nom_ape": "Joel Torres",
"usu_usu": "jtorre",
"usu_cla": "123456",
"usu_cor": "[email protected]",
"usu_cod_sap": "0000105680",
"usu_est": "1",
"usu_cod_rol": 35,
"usu_cod_cli": 1,
"rol_nom": "Vendedor"
},
{
"usu_nom_ape": "Lucia Campos",
"usu_usu": "clucia",
"usu_cla": "123456",
"usu_cor": "[email protected]",
"usu_cod_sap": "0000105680",
"usu_est": "1",
"usu_cod_rol": 43,
"usu_cod_cli": 1,
"rol_nom": "Cliente"
}
]