When concatenating into LINQ returns error

Asked

Viewed 104 times

1

I did this LINQ. In the concatenation between DDD and phone number, which I call phone 1 is giving dick.

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 what is 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.

  • pnet what types of DDD and Phone?

  • int and string, I think that’s it, right? But in a concatenation it doesn’t do the automatic cast?

  • Yeah, the names of the guys are int and string. Yes, the primitive types to string cast is automatic. No need to .ToString() in this case. See the correction I made in my reply. It should help you.

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

Obs: You don’t have to give a Tolist() and then a Firstordefault(), already put the Firstordefault() that will solve the problem, besides being unnecessary Tolist() will bring everything from your table and the Firstordefault() takes the first occurrence in memory, it entails performace.

  • He says he doesn’t recognize Tostring() in Linqtoentities

  • I think I’ll concatenate in jquery and solve this problem. Whenever I try to concatenate or rename a field as I tried to do, it sucks

  • I know, that’s what I did and it keeps going wrong. Concatenating in jquery is easier. Always q try in LINQ gives error, always. I could never do, rename fields like I did, create an Alias

  • Unable to cast the type 'System.Nullable`1' to type 'System.Object'. LINQ to Entities only Supports casting EDM Primitive or enumeration types.

0

Your problem may be with the variable usuario

Change:

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

To:

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

Browser other questions tagged

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