I am trying to enter a list of objects in a datatable in ASP.NET Core, I am joining two tables, the tables are user and role, which I am taking fields from both tables, I use linq to join the fields, it gives me a list of objects and those objects I want to be added to a datatable and to be able to show the list of objects, when sending in postman it gives me an error I want you to show me the users who have the same sap code, but also to show me the name of the role and that field is in the role table, it will return several users
this is my 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;
}
the error it gives me is the following
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)
I want the following to return
[
{
"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"
}
]
The
DataTable
are not serializable objects like JSON, insteadDataTables
use DTOs to store your data.