Conversion error in a query

Asked

Viewed 142 times

6

I made this consultation and in Where is giving this error:

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

That part was missing

string _cnpj = carregaGrid.cnpjRazao;

This is my consultation.

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

What can I do to fix it? I tried a Tostring() and still the error continues. The error is on this line:

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

My complete code

[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);
        }

Try this and no more error. I will test in the code yet, but this will be the way?

var result = resultado.ToList();

            return Json(new { result }, JsonRequestBehavior.AllowGet);
  • Ready!!!!!!!!!

1 answer

3

Thus:

Take off the ToList() of the first and put only when assemble all expressions!

[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);
}
  • This way also gives error. See my example and see if the way is really this.

  • I forgot only put result.Tolist(). I edited again @pnet on Json ...

Browser other questions tagged

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