1

I'm triying to reproduce the next sql command:

SELECT
    p.*,
    ec.*
FROM ut_pto_Persona_m p
     INNER JOIN (
        SELECT
            id_persona,
            MAX(fecha_insert) maxfecha_insert
        FROM ut_pto_PuertaPersonaVisita_m
        WHERE puertaId IN (1039, 1040, 1041, 1042, 1043, 1044, 1045)
        GROUP BY id_persona) b
                ON p.id_persona = b.id_persona
     INNER JOIN ut_pto_sexo_m s ON p.id_sexo = s.id_sexo
     LEFT JOIN ut_pto_EmpresaContratista_m ec ON ec.id = p.id_empresa
WHERE p.id_persona NOT IN (SELECT id_persona FROM ut_pto_Transacciones)

in a linq query using query syntax..

so far this is what i have done:

var personaIds = (from p in context.ut_pto_Transacciones
                  select p.id_persona).ToList();

            var visitas = (from pv in context.ut_pto_PuertaPersonaVisita_ms.AsEnumerable()
                           where pv.fecha_insert != null && pv.id_persona != null && (pv.puertaId == 1039 || pv.puertaId == 1040 || pv.puertaId == 1041 || pv.puertaId == 1042 || pv.puertaId == 1043 || pv.puertaId == 1044 || pv.puertaId == 1045)
                           group pv by pv.id_persona.Value into g
                           let maxFechaInsert = g.Select(z => z.fecha_insert).Max()
                           let p2 = g.First(z => z.fecha_insert == maxFechaInsert)
                           select new Visita
                           {
                               idPersona = p2.id_persona.Value,
                               fechaInicio = p2.FechaInicio,
                               fechaTermino = p2.FechaTermino
                           });

            var transacciones = (from p in context.ut_pto_Persona_ms
                                join v in visitas
                                on p.id_persona equals v.idPersona
                                join s in context.ut_pto_sexo_ms
                                on p.id_sexo equals s.id_sexo
                                join e in context.EmpresaContratistas
                                on p.id_empresa equals e.Id
                                where !personaIds.Contains(p.id_persona)
                                select new PersonaGeneraDTO{
                                    Empresa = e.Nombre,
                                    rut_persona = p.rut_persona,
                                    dig_verificador = p.dig_verificador,
                                    nombres = p.nombres,
                                    apellido_paterno = p.apellido_paterno,
                                    apellido_materno = p.apellido_materno,
                                    fecha_nacimiento = p.fecha_nacimiento,
                                    FechaInicio = (DateTime)v.fechaInicio,
                                    FechaTermino = (DateTime)v.fechaTermino,
                                    sexo = s.desc_sexo.Substring(0, 1),
                                    tarjeta = p.rut_persona,
                                    rut_empresa = e.Rut,
                                    dv_empresa = e.Dv,
                                    ficha = p.rut_persona.ToString(),
                                    id_persona = p.id_persona
                                }).ToList();

but, I'm receiving an error response that I can not find a solution until now:

System.InvalidOperationException: The LINQ expression 'DbSet() .Join( inner: __p_0, outerKeySelector: p => p.id_persona, innerKeySelector: v => v.idPersona, resultSelector: (p, v) => new { p = p, v = v })' 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.

I would really appreciate any help you can give me.

Svyatoslav Danyliv
  • 21,911
  • 3
  • 16
  • 32
  • I have reformatted your SQL and found that LINQ query is doing something different. Also `maxfecha_insert` is not used in SQL as in LINQ query. – Svyatoslav Danyliv Mar 08 '22 at 07:07
  • What's the reason for posting the second LINQ query? It doesn't seem related to the question, so you better remove it. – Gert Arnold Mar 08 '22 at 08:09
  • Sometimes rather than simply trying to translate SQL to Linq, it would be better to start from scratch and try to figure out what information you are trying to return. In this case, your SQL looks overly complicated. As @SvyatoslavDanyliv says maxfecha_insert does not seem to be used and is there really any need to join the table ut_pto_sexo_m. rather than just check if p.id_sexo is not null. – sgmoore Mar 08 '22 at 09:46
  • Why do you have `AsEnumerable()` in your second query. And does it work without it? I would view the error message you are getting as saying that you are asking SQL to do something that is too complicated for it and adding AsEnumerable() to the second query certainly makes the third query at lot more complicated. – sgmoore Mar 08 '22 at 09:52
  • @sgmoore I would say asking LINQ to database not SQL, obviously SQL can handle it. – NetMage Mar 08 '22 at 19:18
  • What LINQ are you using: LINQ to Objects / SQL / EF 6.x / EF Core 2.0 / 2.1 / 3.x / 5.x / 6.x? What database provider? Perhaps my [SQL to LINQ Recipe](https://stackoverflow.com/questions/49245160/sql-to-linq-with-multiple-join-count-and-left-join/49245786#49245786) might help you. – NetMage Mar 08 '22 at 19:19

0 Answers0