Using elements like "Not Exists" and Sub Query in LINQ, LAMBDA EXPRESSION and Nhibernate

0

Good morning, I have a query to run in my database, but here in our projects we use Nhibernate as an ORM tool. I'm having difficulty passing this SQL query to a syntax used with NHibernate.

The query is down:

SELECT distinct a.NumContrOper FROM [tb_viw_cdc_par] a inner join tb_viw_cdc_ope b on a.NumContrOper = b.NumContrOper and a.CodCliente = b.CodCliente where [TipParc] in (1,3,8,10) and not exists (select * from [tb_viw_cdc_par] par where par.[TipParc] in (2,9) and par.NumContrOper = b.NumContrOper and par.CodCliente = b.CodCliente);

Notice that in it, I use a not exists clause and also a subquery to complement my query. I want your help to know how I can mount this subquery using the term 'Not Exists' in NHibernate, Linq or lambda expression sixtaxes.

Here's part of the query I'm getting:

private IQueryable<string> BuscarParcFut(ParcelaAlfandegaEntidadeFiltro aFiltros)
    {
        IQueryable<OperacaoAlfandegaEntidade> operacaoAlfandega = this.Buscar<OperacaoAlfandegaEntidade>();
        List<int?> tiposNot = new List<int?>();

        tiposNot.Add(int.Parse(TipoParcelaEnumerador.ParcelaFutura.ComoString()));
        tiposNot.Add(int.Parse(TipoParcelaEnumerador.ParcelaFuturaConsorcio.ComoString()));


        List<int?> tipos = new List<int?>();
        tipos.Add(int.Parse(TipoParcelaEnumerador.ParcelaAnterior.ComoString()));
        tipos.Add(int.Parse(TipoParcelaEnumerador.PagamentoAvulso.ComoString()));
        tipos.Add(int.Parse(TipoParcelaEnumerador.ParcelaAnteriorConsorcio.ComoString()));
        tipos.Add(int.Parse(TipoParcelaEnumerador.PagamentoAvulsoConsorcio.ComoString()));

        var query = BuscarBasico(aFiltros)
            .Join(operacaoAlfandega,
                                a => new { Contrato = a.Contrato, CpfCnpj = a.CpfCnpj },
                                b => new { Contrato = b.Contrato, CpfCnpj = b.CpfCnpj },
                                (a, b) =>
                                    new
                                    {
                                        Parcela = a,
                                        Operacao = b,
                                    })
                                    .Where(w => tipos.Contains(w.Parcela.TipoParcela)
                                    )
                                    .Select(a => a.Parcela.Contrato)
                                    .Distinct()
                                    ;




        return query;


    }
    
asked by anonymous 06.09.2018 / 14:09

1 answer

0

It would be important if you included your entity and its mapping, so that from this it is easier to identify the modeling and relationships between these two entities.

Is it generating any results in this way? Do you have the SQL that generated this result?

Anyway, with the information you've provided here, my suggestion is a code like this (just fit the methods you use):

List<int?> tipos = new List<int?>();
tipos.Add(int.Parse(TipoParcelaEnumerador.ParcelaAnterior.ComoString()));
tipos.Add(int.Parse(TipoParcelaEnumerador.PagamentoAvulso.ComoString()));
tipos.Add(int.Parse(TipoParcelaEnumerador.ParcelaAnteriorConsorcio.ComoString()));
tipos.Add(int.Parse(TipoParcelaEnumerador.PagamentoAvulsoConsorcio.ComoString()));

IQueryable<Operacao> queryOperacoes = Buscar<Operacao>();

IQueryable<Parcela> queryParcelas = BuscarBasico(aFiltros)
  .Where(w => tipos.Contains(w.Parcela.TipoParcela) && 
              !queryOperacoes.Any(o => o.Contrato == w.Contrato && o.CpfCnpj == w.CpfCnpj));

var resultado = queryParcelas.Select(s => s.Parcela.Contrato).Distinct().ToList();

I removed the JOIN from the code because you are not using it in Select. It includes in the WHERE an Any check in the IQueryable of Operations, where basically it will do the following at the end: bring me the plots that have no operation given a contract and cpf / cnpj. later only the contract number is obtained.

    
28.09.2018 / 14:14