首页 > 解决方案 > 42P18: 无法确定参数 $12 的数据类型

问题描述

我正在尝试运行此查询,但它告诉我此错误:

42P18: 无法确定参数 $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();

错误发生在下面这一行,如果我退出,工作正常,我试图改变,但是我需要这个过滤器。我不明白我做错了什么。

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

这是我填写变量的方式

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

知道如何解决吗?谢谢你。

标签: c#postgresqlwinformslinq

解决方案


尝试分离过滤。这样的参数不利于 LINQ 翻译和性能。我希望它能解决你的问题。

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();

推荐阅读