Method does not translate to SQL when used in another method

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.

New contributor

German David is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật