6

Fiz essa consulta e no Where está dando esse erro:

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<AnonymousType#1>' to 'System.Collections.Generic.List<AnonymousType#1>'

Faltou essa parte

string _cnpj = carregaGrid.cnpjRazao;

Essa é minha consulta linq.

var resultado = (from web in db.T_PDV

                             join testab in db.T_TipoEstabelecimento on web.IDTipoEstabelecimento equals (testab.IDTipoEstabelecimento)
                             join trede in db.T_TipoRede on web.IDTipoRede equals (trede.IDTipoRede)
                             join tusupdv in db.T_UsuarioPDV on web.IDPdv equals (tusupdv.IDPDV)
                             join tusu in db.T_Usuario on tusupdv.IDUsuario equals (tusu.IDUsuario)
                             join tstatus in db.T_CRM_StatusPDV on web.CNPJ equals (tstatus.DE_Cnpj)

                             select new
                             {
                                 web.CNPJ,
                                 web.RazaoSocial,
                                 web.NomeFantasia,
                                 web.Endereco,
                                 web.Bairro,
                                 web.Cidade,
                                 web.Estado,
                                 web.CEP,
                                 web.Complemento,
                                 web.Numero,
                                 web.QtdeCheckOuts,
                                 web.Telefone,
                                 web.NomeRede,
                                 web.Email,
                                 web.Celular,
                                 web.IS_Ativo,
                                 tstatus.IT_Status,
                                 trede.Nome,
                                 contato = tusu.Nome,
                                 cel_contato = tusu.Celular
                             }).ToList();

            if (!string.IsNullOrEmpty(_cnpj))
                resultado = resultado.Where(cn => cn.CNPJ == _cnpj);

O que posso fazer para resolver? Tentei um ToString() e mesmo assim o erro continua. O erro é nessa linha:

if (!string.IsNullOrEmpty(_cnpj))
                    resultado = resultado.Where(cn => cn.CNPJ == _cnpj);

Meu código completo

[HttpPost]
        public JsonResult MontaGridPdv(carregaGridPesquisa carregaGrid)//
        {
            ConsultaGeral geral = new ConsultaGeral();
            V99_WEBEntities db = new V99_WEBEntities();
            V99_QAEntities dba = new V99_QAEntities();

            string[] arrayCnpj = null;
            string[] arrayRazao = null;
            int cont = 0;
            var resultado = (from web in db.T_PDV

                             join testab in db.T_TipoEstabelecimento on web.IDTipoEstabelecimento equals (testab.IDTipoEstabelecimento)
                             join trede in db.T_TipoRede on web.IDTipoRede equals (trede.IDTipoRede)
                             join tusupdv in db.T_UsuarioPDV on web.IDPdv equals (tusupdv.IDPDV)
                             join tusu in db.T_Usuario on tusupdv.IDUsuario equals (tusu.IDUsuario)
                             join tstatus in db.T_CRM_StatusPDV on web.CNPJ equals (tstatus.DE_Cnpj)

                             select new
                             {
                                 web.CNPJ,
                                 web.RazaoSocial,
                                 web.NomeFantasia,
                                 web.Endereco,
                                 web.Bairro,
                                 web.Cidade,
                                 web.Estado,
                                 web.CEP,
                                 web.Complemento,
                                 web.Numero,
                                 web.QtdeCheckOuts,
                                 web.Telefone,
                                 web.NomeRede,
                                 web.Email,
                                 web.Celular,
                                 web.IS_Ativo,
                                 tstatus.IT_Status,
                                 trede.Nome,
                                 contato = tusu.Nome,
                                 cel_contato = tusu.Celular
                             });

            if (!string.IsNullOrEmpty(_cnpj))
                resultado = resultado.Where(cn => cn.CNPJ == carregaGrid.cnpjRazao);
            if (!string.IsNullOrEmpty(carregaGrid.contato))
                resultado = resultado.Where(cn => cn.Contato == carregaGrid.contato);
            if (!string.IsNullOrEmpty(carregaGrid.rede))
                resultado = resultado.Where(cn => cn.Rede == carregaGrid.rede);

            return Json(new { resultado }, JsonRequestBehavior.AllowGet);
        }

Tente isso e não deu mais erro. Vou testar no código ainda, mas será esse o caminho?

var result = resultado.ToList();

            return Json(new { result }, JsonRequestBehavior.AllowGet);
1
  • Pronto!!!!!!!!!
    – user6026
    26/06/2014 às 20:33

1 Resposta 1

3

Assim:

Tire o ToList() do primeiro e coloque só quando montar todas as expressões!

[HttpPost]
public JsonResult MontaGridPdv(carregaGridPesquisa carregaGrid)
{
    ConsultaGeral geral = new ConsultaGeral();
    V99_WEBEntities db = new V99_WEBEntities();
    V99_QAEntities dba = new V99_QAEntities();

    string[] arrayCnpj = null;
    string[] arrayRazao = null;
    int cont = 0;

    var resultado = (from web in db.T_PDV
                     join testab in db.T_TipoEstabelecimento on web.IDTipoEstabelecimento equals (testab.IDTipoEstabelecimento)
                     join trede in db.T_TipoRede on web.IDTipoRede equals (trede.IDTipoRede)
                     join tusupdv in db.T_UsuarioPDV on web.IDPdv equals (tusupdv.IDPDV)
                     join tusu in db.T_Usuario on tusupdv.IDUsuario equals (tusu.IDUsuario)
                     join tstatus in db.T_CRM_StatusPDV on web.CNPJ equals (tstatus.DE_Cnpj)
                     select new
                     {
                         web.CNPJ,
                         web.RazaoSocial,
                         web.NomeFantasia,
                         web.Endereco,
                         web.Bairro,
                         web.Cidade,
                         web.Estado,
                         web.CEP,
                         web.Complemento,
                         web.Numero,
                         web.QtdeCheckOuts,
                         web.Telefone,
                         web.NomeRede,
                         web.Email,
                         web.Celular,
                         web.IS_Ativo,
                         tstatus.IT_Status,
                         trede.Nome,
                         contato = tusu.Nome,
                         cel_contato = tusu.Celular
                     }).AsQueryable();

    if (!string.IsNullOrEmpty(_cnpj))
        resultado = resultado.Where(cn => cn.CNPJ == carregaGrid.cnpjRazao).AsQueryable();
    if (!string.IsNullOrEmpty(carregaGrid.contato))
        resultado = resultado.Where(cn => cn.Contato == carregaGrid.contato).AsQueryable();
    if (!string.IsNullOrEmpty(carregaGrid.rede))
        resultado = resultado.Where(cn => cn.Rede == carregaGrid.rede).AsQueryable();

    return Json(resultado.ToList() , JsonRequestBehavior.AllowGet);
}
2
  • Dessa forma também dá erro. Veja o meu exemplo e veja se o caminho é realmente esse.
    – pnet
    26/06/2014 às 21:10
  • Eu esqueci só coloque resultado.ToList(). Editei novamente @pnet no return Json ...
    – user6026
    26/06/2014 às 22:48

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .