Load variable with Split Expressions by scrolling through a list

Asked

Viewed 71 times

0

First I made this code:

for (int i = 0; i < vlstAcessos.Count - 1; i++)
                        {
                            if (vlstAcessos[i].DsURLTransacao == "frmANAAguardeProcesso.aspx")
                                transacaoANA = vlstAcessos[i].DsURLTransacao = "frmANAAguardeProcesso.aspx";
                            else if (vlstAcessos[i].DsURLTransacao == "frmPVListaProcessos.aspx")
                                transacaoPV = vlstAcessos[i].DsURLTransacao = "frmPVListaProcessos.aspx";
                            else if (vlstAcessos[i].DsURLTransacao == "frmGESStatusPriorizar.aspx")
                                transacaoGestor = vlstAcessos[i].DsURLTransacao = "frmGESStatusPriorizar.aspx";
                            else if (vlstAcessos[i].DsURLTransacao == "frmConsultarProcessos.aspx")
                                transacaoConsulta = vlstAcessos[i].DsURLTransacao = "frmConsultarProcessos.aspx";
                        }

Then I saw that this code is ugly and I did this:

transacaoPV = vlstAcessos.Any(l => l.DsURLTransacao == "frmPVListaProcessos.aspx").ToString();

I know perfectly that the lambda expression above will return a True or False. The question is: Is it possible with a Lambda I load a string variable with the expression value? That is, transacaoPV take the value of: frmPVListaProcessos.aspx These both replace the above IF, right?

transacaoGestor   = vlstAcessos.Select(l => l.DsURLTransacao).FirstOrDefault(d => d == "frmGESStatusPriorizar.aspx");
                        transacaoPV       = vlstAcessos.Select(l => l.DsURLTransacao).FirstOrDefault(d => d == "frmPVListaProcessos.aspx");
                        transacaoANA      = vlstAcessos.Select(l => l.DsURLTransacao).FirstOrDefault(d => d == "frmANAAguardeProcesso.aspx");
                        transacaoConsulta = vlstAcessos.Select(l => l.DsURLTransacao).FirstOrDefault(d => d == "frmConsultarProcessos.aspx");

1 answer

1


To be very brief, you could replace

transacaoPV = vlstAcessos.Any(l => l.DsURLTransacao == "frmPVListaProcessos.aspx").ToString();

so here:

transacaoPV = vlstAcessos.Select(l => l.DsURLTransacao)
                         .FirstOrDefault(d=> d == "frmPVListaProcessos.aspx");

An addendum is that the default of the string (and any other non-primitive class) is null, so the variable transacaoPV will be null if you do not find the string in question.

NOTE: I just looked at the text of the question title and although I understood what you meant, it is a bit confusing.

  • I will edit. In fact it should be inside the body the title.

  • I can then say that I can substitute the previous if with those two, right? It’s cleaner code.

Browser other questions tagged

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