Doubt on how to select a record via Inq

Asked

Viewed 623 times

0

I have a chart, where I only record scripts. There is a field, where I separate whether it is technical script or central script. There is a field TipoScript char e is "C" or "T". This table was not well made, without thinking about the future, so we have the field NomeScript, that always has the same name for script Técnico and the same name for Central script.

My consultation should return both scripts, but I’ll have to differentiate between the two. I confess I don’t know how to do it. I could put it like this: tecnico = sc.scripttecnico, but the only way to know is through the countryside TipoScript and name field as I did above, has given dick.

Could anyone suggest me how to do that? It might be on lambda also.

This is my LINQ:

public TPDV getCnpjParceiro(string cnpj)
        {
            WEBEntities db = new WEBEntities();
            TPDV pdv = new TPDV();
            List<string> lista = new List<string>();

            var resultado = (from _lista in db.T_PDV
                             where _lista.CNPJ == cnpj
                             join _st in db.T_CRM_StatusPDV on _lista.CNPJ equals(_st.DE_Cnpj)
                             join _sc in db.T_Script on _st.IT_Status equals((int)_sc.TipoStatus)
                             select new
                             {
                                 _lista.CNPJ,
                                 _lista.RazaoSocial,
                                 _lista.Endereco,
                                 _lista.CaminhoLogo,
                                 _lista.Bairro,
                                 _lista.Cidade,
                                 _st.IT_Status,
                                 _st.DT_TransacaoV,
                                 _sc.Script
                             }).ToList();

            foreach (var lis in resultado)
            {
                pdv.CNPJ = lis.CNPJ;
                pdv.RazaoSocial = lis.RazaoSocial;
                pdv.Endereco = lis.Endereco;
                pdv.CaminhoLogo = lis.CaminhoLogo;
                pdv.Bairro = lis.Bairro;
                pdv.Cidade = lis.Cidade;
            }

            return pdv;

        }

I tried to do it like this, but it makes a mistake in technical and central

var resultado = (from _lista in db.T_PDV
                             where _lista.CNPJ == cnpj
                             join _st in db.T_CRM_StatusPDV on _lista.CNPJ equals(_st.DE_Cnpj)
                             join _sc in db.T_Script on _st.IT_Status equals((int)_sc.TipoStatus)
                             select new
                             {
                                 _lista.CNPJ,
                                 _lista.RazaoSocial,
                                 _lista.Endereco,
                                 _lista.CaminhoLogo,
                                 _lista.Bairro,
                                 _lista.Cidade,
                                 _st.IT_Status,
                                 _st.DT_TransacaoV,
                                 tecnico = _sc.TipoScript == "T" ? _sc.Script : null,
                                 central = _sc.TipoScript == "C" ? _sc.Script : null
                             }).ToList();
  • Is making a mistake ?

2 answers

3

Use models and return them in your LINQ query.

Model

public class Script
{
    public string TipoScript { get; set; }
    public string Script { get; set; }
}

LINQ consultation

var resultado = from s in db.Script select s;

resultado will be a IQueryable<Script>. From there just use in View and do the type test

@model IQueryable<Path.Completo.Da.Classe.Script>

@foreach(var item in Model)
{
    if (item.TipoScript == "C")
    {
        // Lógica para Central
    } else
    {
        // Lógica para técnico
    }
}
  • I have doubts about that. In fact it will be exported via web service through an object, which in my case is of the PDV type. That is the doubt. I’ll edit my post and show you my blog

  • I posted this version because you used the tag asp.net-mvc-5

  • It’s true, I didn’t even realize it, really. I’ll withdraw, I don’t even know if I can

0

The way I solved it was. I did the Technical and Central pata scripts separately and then added to the object.

Browser other questions tagged

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