Place a decision control inside a lambda expression

Asked

Viewed 116 times

2

I’m building this consultation:

private List<string> pegaInformacaoParceiro(string _osparceiro, string _cnpj)
        {
            List<string> lista = new List<string>();
            WEBEntities db = new WEBEntities();

            var resultado = db.T_PDV
                            .Join(db.T_CRM_StatusPDV, t1 => t1.CNPJ, t2 => t2.DE_Cnpj, (t1, t2) => new { t1, t2 })
                            .Join(db.T_TarefaParceiro, p1 => p1.t1.CNPJ, p2 => p2.CNPJ, (p1, p2) => new { p1, p2 })
                            .Join(db.T_OsParceiro, o1 => o1.p2.IDTarefaParceiro, o2 => o2.IDTarefaParceiro, (o1, o2) => new { o1, o2 })
                            .Join(db.T_Acao, a1 => a1.o1.p2.IDAcao, a2 => a2.IDAcao, (a1, a2) => new { a1, a2 })
                            .Join(db.T_ProximaAcao, x1 => x1.a2.IDAcao, x2 => x2.IDAcao, (x1, x2) => new { x1, x2})
                            .Where(cn => cn.x1.a1.o2.NumOs == Convert.ToInt32(_osparceiro))
                            .Select(i => new { });

            return lista;
        }

I would like if _cnpj were NULL or Empty, load as is, otherwise I will do a Where with _cnpj and _osparceiro. It is like?

1 answer

1


I’m not sure what the comparison with _cnpj, but I think the line below is very close to what you need.

.Where(cn => (cn.x1.a1.o2.NumOs == Convert.ToInt32(_osparceiro)) && (_cnpj == "" || cn.cnpj == _cnpj))
  • I don’t think I made myself clear. I only leave to compare the cnpj, if the same is not null or not empty, because it is a way for me to have a query only by a parameter or both.

  • But then. The first part checks whether it is null or empty, ie, _cnpj == "" (String is type by reference, so the null always returns empty). If it is not empty, check the next condition, in case, cn.cnpj == _cnpj. Got it?

  • is understood. That sometimes cuckoo processing has a delay.

Browser other questions tagged

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