Linq query with Count

Asked

Viewed 187 times

3

I’m doing a query that has some joins and a Count, but I want to do it with Linq.

This is my Query.

sbSQL.Append("SELECT grproj.cdgruproj as  Id, grproj.dsgruproj as DescricaoGrupoProjeto, ");
            sbSQL.Append("(SELECT COUNT(*) ");
            sbSQL.Append("        FROM usufrm ");
            sbSQL.Append("        WHERE usufrm.cdfrm = form.cdfrm ");
            sbSQL.Append("        AND usufrm.sgproj = proj.sgproj ");
            sbSQL.Append("        AND usufrm.tpproj = tproj.tpproj ");
            sbSQL.Append("        AND usufrm.dtcan IS NULL) as QuantidadeDeUsuarios ");
            sbSQL.Append("FROM grproj  ");
            sbSQL.Append("INNER JOIN proj ON grproj.cdgruproj = proj.cdgruproj ");
            sbSQL.Append("INNER JOIN tproj ON proj.tpproj = tproj.tpproj ");
            sbSQL.Append("INNER JOIN projfr ON proj.sgproj = projfr.sgproj AND proj.tpproj = projfr.tpproj ");
            sbSQL.Append("INNER JOIN form ON form.cdfrm = projfr.cdfrm ");
            sbSQL.Append("WHERE grproj.dtcan IS NULL AND proj.dtcan IS NULL AND form.dtcan IS NULL ");

This is the example of my query with Linq, the quantity field should be a Count:

 IList<GrupoProjetoEntity> grupos = (from grupoProjeto in session.Query<GrupoProjetoEntity>()
                                       join projeto in session.Query<ProjetoEntity>() on grupoProjeto.Id equals projeto.cdgruproj.Id
                                       join tipoProjeto in session.Query<TipoProjetoEntity>() on projeto.tpproj.TipoProjeto equals tipoProjeto.TipoProjeto
                                       join projetoFormulario in session.Query<ProjetoFormularioEntity>() on
                                       new { SiglaProjeto = projeto.sgproj, tpproj = projeto.tpproj.TipoProjeto }
                                       equals new { SiglaProjeto = projetoFormulario.sgproj, tpproj = projetoFormulario.tpproj.TipoProjeto }
                                       join formulario in session.Query<FormularioEntity>() on projetoFormulario.cdfrm.Id equals formulario.Id
                                       select new GrupoProjetoEntity()
                                       {
                                           Id = grupoProjeto.Id,
                                           DescricaoGrupoProjeto = grupoProjeto.DescricaoGrupoProjeto,
                                           QuantidadeDeUsuarios = 0
                                       }).ToList();

1 answer

3


Try doing a subquery and get the Count hers:

IList<GrupoProjetoEntity> grupos = (from grupoProjeto in session.Query<GrupoProjetoEntity>()
    join projeto in session.Query<ProjetoEntity>() on grupoProjeto.Id equals projeto.cdgruproj.Id
    join tipoProjeto in session.Query<TipoProjetoEntity>() on projeto.tpproj.TipoProjeto equals tipoProjeto.TipoProjeto
    join projetoFormulario in session.Query<ProjetoFormularioEntity>() on
    new { SiglaProjeto = projeto.sgproj, tpproj = projeto.tpproj.TipoProjeto }
    equals new { SiglaProjeto = projetoFormulario.sgproj, tpproj = projetoFormulario.tpproj.TipoProjeto }
    join formulario in session.Query<FormularioEntity>() on projetoFormulario.cdfrm.Id equals formulario.Id
    let contagem = (
                    from usuario in session.Query<UsuarioEntity>()
                    where usuario.cdfrm == formulario.cdfrm
                    where usuario.sgproj = projeto.sgproj
                    where usuario.tpproj = tipoProjeto.tpproj
                    where usuario.dtcan == null
                   ).Count()
    select new GrupoProjetoEntity()
    {
       Id = grupoProjeto.Id,
       DescricaoGrupoProjeto = grupoProjeto.DescricaoGrupoProjeto,
       QuantidadeDeUsuarios = contagem
    }).ToList();
  • Very good Miguel, as slow as it was, the consultation worked.

  • What not... try to use let to calculate the value of Count before the new... may be that when using the operator Count within the new he’s doing several Count.

  • You can also see which query is being generated and run in the database using some BD profiler.

  • So, the consultation in the bank is instantaneous, I think for the excess of joins I will try to use Let and I speak to you.

  • All right, if it gets any faster I’ll change the answer to make it as good as possible.

  • What would this query look like with Let? it serves to group?

  • let is just a variable in the middle of the query, which can be used later. I changed the answer, but check there. If it doesn’t work I go back to the way it was before.

  • Not much has changed in speed, I think I’m going to leave for an HQL, or something faster. Thank you Miguel!!

  • As you mentioned HQL, I assume you’re using Nhibernate. There are ways to see the query it is running in BD(is in English)... so it is easier to know what is happening under the cloths.

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.