Doubt on how to select a record via linq

0

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

My query should return both scripts , but I'll have to differentiate between them. I confess I do not know how to do this. I could put it like this: tecnico = sc.scripttecnico , but the only way to know is by field TipoScript and name field as I did above, has given stick.

Could anyone suggest me how to do this? It can be in lambda as well.

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 this, but it gives an error 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();
    
asked by anonymous 21.05.2014 / 20:43

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 query

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

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

@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
    }
}
    
21.05.2014 / 21:03
0

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

    
30.05.2014 / 11:54