Mari
Mari

Reputation: 69

42P18: could not determine data type of parameter $12

I'm trying to run this query, but it tells me this error:

42P18: could not determine data type of parameter $12

 bds.DataSource = (from vendas in conexao.Vendas
                                  join nota in conexao.NotaFiscal on  vendas.IdNotaFIscal equals nota.Id
                                  join clientes in conexao.Cliente on vendas.IdCliente equals clientes.Id
                                 where (nota.DataEmissao >= periodoInicial || periodoInicial == null) &&
                                  (nota.DataEmissao <= periodoFinal || periodoFinal == null) &&
                                  (clientes.Id == idCliente || idCliente == null) &&
                                   (nota.NumeroNotaFiscal >= notaInicial || notaInicial == null) &&
                                   (nota.NumeroNotaFiscal <= notaFinal || notaFinal == null) &&
                                  (nota.Modelo == mod || mod == null)
                                  orderby nota.Id descending
                             select new 
                             {
                                 Id = nota.Id,
                                 ClienteId = clientes.NomeRazao,
                                 Chave = nota.Chave,
                                 DataEmissao = nota.DataEmissao,
                                 Status = nota.Status,
                                 NumeroNota = nota.NumeroNotaFiscal,
                                 Modelo = nota.Modelo,
                             }).ToList();

The error occurs in this line below, if I withdraw, works normal, I have tried to change, however I need this filter.I can't understand what I'm doing wrong.

  (nota.Modelo == mod || mod == null)

And here's how I fill out the variable

  string mod = String.Empty; 
            if(modelo != "TODOS") mod = modelo == "NF-E" ? "55" : modelo== "NFC-e" ? "65" : null;

Any idea how I can fix it? Thank you.

Upvotes: 0

Views: 432

Answers (1)

Svyatoslav Danyliv
Svyatoslav Danyliv

Reputation: 27461

Try to separate filtering. Such parameters is bad for LINQ Translation and performance. I hope it will solve your problem.

var query = 
   from vendas in conexao.Vendas
   join nota in conexao.NotaFiscal on  vendas.IdNotaFIscal equals nota.Id
   join clientes in conexao.Cliente on vendas.IdCliente equals clientes.Id
   select new { vendas, nota, clientes };

if (periodoInicial != null)
   query = query.Where(x => x.nota.DataEmissao >= periodoInicial);
if (periodoFinal != null
   query = query.Where(x => x.nota.DataEmissao <= periodoFinal);
if (idCliente != null)
   query = query.Where(x => x.clientes.Id == idCliente);
if (notaInicial != null)
   query = query.Where(x => x.nota.NumeroNotaFiscal >= notaInicial);
if (notaFinal != null)
   query = query.Where(x => x.nota.NumeroNotaFiscal <= notaFinal);
if (mod != null)
   query = query.Where(x => x.nota.Modelo == mod);

var result = 
   from q in query
   orderby q.nota.Id descending
   select new
   {
       Id = q.nota.Id,
       ClienteId = q.clientes.NomeRazao,
       Chave = q.nota.Chave,
       DataEmissao = q.nota.DataEmissao,
       Status = q.nota.Status,
       NumeroNota = q.nota.NumeroNotaFiscal,
       Modelo = q.nota.Modelo,
   };

bds.DataSource = result.ToList();

Upvotes: 1

Related Questions