When concatenating in LINQ I get error

1

I made this LINQ. In the concatenation between DDD and phone number, which I call phone1, it is flicking.

var agendamento = (
    from pdv in db.T_PDV
    from tarefa in db.T_TarefaParceiro.Where(trf => trf.CNPJ == pdv.CNPJ).DefaultIfEmpty()
    from parceiro in db.T_OsParceiro.Where(prf => prf.IDTarefaParceiro == tarefa.IDTarefaParceiro)
    from acao in db.T_Acao.Where(ac => ac.IDAcao == tarefa.IDAcao).DefaultIfEmpty()
    from proxima in db.T_ProximaAcao.Where(pxm => pxm.IDAcao == acao.IDAcao).DefaultIfEmpty()
    from info in db.T_InfoClientePdv.Where(inf => inf.CNPJ == pdv.CNPJ).DefaultIfEmpty()
    from cliente in db.T_Cliente.Where(clie => clie.IDCliente == info.IDCliente).DefaultIfEmpty()
    from usuariopdv in db.T_UsuarioPDV.Where(usu => usu.IDPDV == pdv.IDPdv)
    from usuario in db.T_Usuario.Where(us => us.IDUsuario == usuariopdv.IDUsuario)

    where pdv.CNPJ == _agendamento.Cnpj //&& parceiro.NumOs == _agendamento.Os

    orderby parceiro.DataVisita descending

    select new
    {
     pdv.CNPJ,
     pdv.DataCadastro,
     cliente.RazaoSocial,
     acao.Acao,
     proxima.ProximaAcao,
     parceiro.IDOsParceiro,
     parceiro.NumOs,
     parceiro.DataVisita,
     parceiro.DataAgendamento,
     parceiro.Tecnico,
     usuario.Nome,
     telefone1 = "(" + usuario.DDD + ")" + usuario.Telefone
    })
    .ToList()
    .FirstOrDefault();

The error you are giving is the following:

  

Unable to cast the type 'System.Nullable'1' to type 'System.Object'. LINQ to Entities only supports casting EDM primitive or enumeration types.

    
asked by anonymous 16.06.2014 / 20:54

2 answers

1

Put it like this:

var agendamento = (
    from pdv in db.T_PDV
    from tarefa in db.T_TarefaParceiro.Where(trf => trf.CNPJ == pdv.CNPJ).DefaultIfEmpty()
    from parceiro in db.T_OsParceiro.Where(prf => prf.IDTarefaParceiro == tarefa.IDTarefaParceiro)
    from acao in db.T_Acao.Where(ac => ac.IDAcao == tarefa.IDAcao).DefaultIfEmpty()
    from proxima in db.T_ProximaAcao.Where(pxm => pxm.IDAcao == acao.IDAcao).DefaultIfEmpty()
    from info in db.T_InfoClientePdv.Where(inf => inf.CNPJ == pdv.CNPJ).DefaultIfEmpty()
    from cliente in db.T_Cliente.Where(clie => clie.IDCliente == info.IDCliente).DefaultIfEmpty()
    from usuariopdv in db.T_UsuarioPDV.Where(usu => usu.IDPDV == pdv.IDPdv)
    from usuario in db.T_Usuario.Where(us => us.IDUsuario == usuariopdv.IDUsuario)

    where pdv.CNPJ == _agendamento.Cnpj //&& parceiro.NumOs == _agendamento.Os

    orderby parceiro.DataVisita descending

    select new
    {
     pdv.CNPJ,
     pdv.DataCadastro,
     cliente.RazaoSocial,
     acao.Acao,
     proxima.ProximaAcao,
     parceiro.IDOsParceiro,
     parceiro.NumOs,
     parceiro.DataVisita,
     parceiro.DataAgendamento,
     parceiro.Tecnico,
     usuario.Nome,
     telefone1 = "(" + usuario.DDD ?? "00" + ")" + usuario.Telefone
    })    
    .FirstOrDefault();

Note: No need to give ToList (), and then a FirstOrDefault () , already place the FirstOrDefault () that will solve the problem, in addition to being unnecessary ToList () will bring everything from your table and the FirstOrDefault () takes the first occurrence in memory, this results in performace.

    
16.06.2014 / 20:59
0

It may be that your problem is with the variable usuario

Change:

telefone1 = "(" + usuario.DDD + ")" + usuario.Telefone

To:

telefone1 = usuario == null ? "não informado" : "(" + usuario.DDD + ")" + usuario.Telefone
    
16.06.2014 / 21:01