Best regard. At this time we are carrying out a method that allows us to identify which data has been entered into a table and which has not. Because to do this within our logic, we must do a join, we look for the data table with which we are going to evaluate whether the data exists, has unique values so that the query does not present timeout errors. Keeping this in mind, the first thing we do is a method that gives us a table with unique values in which we do a group by like this:
/// <summary>
/// 2024-07-10: Agrupar los datos por equipo en la tabla de historico de llantas para obtener un solo registro
/// </summary>
/// <returns>Lista de equipos</returns>
[HttpGet("/Llantas/Salidas/Equipos/Unicos", Name = "GetLlantasSalidasEquiposUnicos")]
public List<TemListaTextoString> GetLlantasSalidasEquiposUnicos()
{
try
{
var q = from a in context.LlanHistoria
group a by a.equipo into GrupoA
select new TemListaTextoString
{
TextoString = GrupoA.Key
};
return q.ToList();
}
catch
{
// 2024-07-10: Utilizo esta excepción para no crear mas excesiones y con esta puedo mostrar informacion
// en caso de errores.
return ObrasMantenimientoExcepcion;
}
}
After doing this method, we create the method in which we will invoke it to do the join. This would be the following method:
/// <summary>
/// 2024-07-09: Crear la tabla que permita tener un campo que permita idetificar los equipos a revisar llantas.
/// </summary>
/// <returns>Tabla de equipos a revisar llantas</returns>
[HttpGet("/Llantas/Mantenimientos/Equipos/Revisar", Name = "GetLlantasMantenimientosEquiposRevisar")]
public List<TemGeneMttoPrevObras> GetLlantasMantenimientosEquiposRevisar()
{
try
{
var t = GetLlantasSalidasEquiposUnicos();
var q = from a in context.GeneMttoPrev
// 2024-07-09: Join entre dos tablas (tabla a y tabla b1).
join b1 in t on
a.equipo
equals
b1.TextoString
into GrupoB
// 2024-07-09: Esto lo hago para que que sean LEFT JOIN.
from b in GrupoB.DefaultIfEmpty()
select new TemGeneMttoPrevObras
{
Registro = a.Registro,
equipo = a.equipo,
bodega = a.bodega,
codigo_mantenimiento = a.codigo_mantenimiento,
ciclo = a.ciclo,
frecuencia_Mantenimiento = a.frecuencia_Mantenimiento,
unidad_frecuencia = a.unidad_frecuencia,
fecha_estimada = a.fecha_estimada,
fecha_mantenimiento = a.fecha_mantenimiento,
horometro_estimado = a.horometro_estimado,
horometro_mantenimiento = a.horometro_mantenimiento,
horometro_final = a.horometro_final,
resultado_muestreo = a.resultado_muestreo,
emp = a.emp,
// 2024-07-08: Debido a que en la clase o mejor dicho en C# no esposible tener
// datos tipo DateTime como null, se procede a modificar el dato.
FechaInsert = new DateTime(1900, 01, 01),
// 2024-07-09: Para no crear una clase para este metodo se aprovecha uno ya creado pero
// se inhabilitan el campo Obra, el cual se calcula realmente en otro metodo.
Obra = "NoAplica",
// 2024-06-21: Se valida si hay registros en la tabla indicada.
Estado = b == null ? "NoAplica" : "Aplica"
};
return q.ToList();
}
catch (Exception)
{
return TemGeneMttoPrevObrasExcepcion;
}
}
We proceed to validate the method and identify that it makes a jump to the exception catch area. Due to this, we seek to delve a little deeper and debug said method, finding the following error:
System.InvalidOperationException: 'The LINQ expression 'DbSet<GeneMttoPrev>()
.LeftJoin(
inner: __p_0
.AsQueryable(),
outerKeySelector: a => a.equipo,
innerKeySelector: b1 => b1.TextoString,
resultSelector: (a, b) => new TemGeneMttoPrevObras{
Registro = a.Registro,
equipo = a.equipo,
bodega = a.bodega,
codigo_mantenimiento = a.codigo_mantenimiento,
ciclo = a.ciclo,
frecuencia_Mantenimiento = a.frecuencia_Mantenimiento,
unidad_frecuencia = a.unidad_frecuencia,
fecha_estimada = a.fecha_estimada,
fecha_mantenimiento = a.fecha_mantenimiento,
horometro_estimado = a.horometro_estimado,
horometro_mantenimiento = a.horometro_mantenimiento,
horometro_final = a.horometro_final,
resultado_muestreo = a.resultado_muestreo,
emp = a.emp,
FechaInsert = new DateTime(
1900,
1,
1
),
Obra = "NoAplica",
Estado = b == null ? "NoAplica" : "Aplica"
}
)' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.'
In this way, it is identified that the code we are writing in C# LinQ is not being translated into SQL and therefore the query cannot be completed correctly. We suspect that the error is when calling the GetLlantasSalidasEquiposUnicos() method in the GetLlantasMantenimientosEquiposRevisar() method, we think it is when doing the join here:
join b1 in t on
a.equipo
equals
b1.TextoString
into GrupoB
// 2024-07-09: Esto lo hago para que que sean LEFT JOIN.
from b in GrupoB.DefaultIfEmpty()
But despite this, we see no other way to do it.
We have evaluated writing the same query in different ways to see if we can get it to work, this is another way to do it but it still gives the same error:
/// <summary>
/// 2024-07-09: Crear la tabla que permita tener un campo que permita idetificar los equipos a revisar llantas.
/// </summary>
/// <returns>Tabla de equipos a revisar llantas</returns>
[HttpGet("/Llantas/Mantenimientos/Equipos/Revisar", Name = "GetLlantasMantenimientosEquiposRevisar")]
public List<TemGeneMttoPrevObras> GetLlantasMantenimientosEquiposRevisar()
{
try
{
var t = GetLlantasHistoricoListaEquipos();
var q = from a in context.GeneMttoPrev
// 2024-07-09: Join entre dos tablas (tabla a y tabla b1).
join b1 in t on new TemListaTextoString
{
TextoString = a.equipo
}
equals new TemListaTextoString
{
TextoString = b1.TextoString
}
into GrupoB
// 2024-07-09: Esto lo hago para que que sean LEFT JOIN.
from b in GrupoB.DefaultIfEmpty()
select new TemGeneMttoPrevObras
{
Registro = a.Registro,
equipo = a.equipo,
bodega = a.bodega,
codigo_mantenimiento = a.codigo_mantenimiento,
ciclo = a.ciclo,
frecuencia_Mantenimiento = a.frecuencia_Mantenimiento,
unidad_frecuencia = a.unidad_frecuencia,
fecha_estimada = a.fecha_estimada,
fecha_mantenimiento = a.fecha_mantenimiento,
horometro_estimado = a.horometro_estimado,
horometro_mantenimiento = a.horometro_mantenimiento,
horometro_final = a.horometro_final,
resultado_muestreo = a.resultado_muestreo,
emp = a.emp,
// 2024-07-08: Debido a que en la clase o mejor dicho en C# no esposible tener
// datos tipo DateTime como null, se procede a modificar el dato.
FechaInsert = new DateTime(1900, 01, 01),
// 2024-07-09: Para no crear una clase para este metodo se aprovecha uno ya creado pero
// se inhabilitan el campo Obra, el cual se calcula realmente en otro metodo.
Obra = "NoAplica",
// 2024-06-21: Se valida si hay registros en la tabla indicada.
Estado = b == null ? "NoAplica" : "Aplica"
};
return q.ToList();
}
catch (Exception)
{
return TemGeneMttoPrevObrasExcepcion;
}
}
The important thing about all this is to identify the cause of the failure and how it is the correct way to do it from Linq, since we have really liked working from the client side.
We appreciate the help you can give us.
We hope to be able to identify the novelty that does not allow the query to be executed, since we have also tried not to do the initial groub by, changing this for a table where the data is unique and helps the query to be executed efficiently, but despite Therefore, the error persists. Within the question, we have shown the things that we have done without having the desired results.
German David is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.