Load variable with Lambda expressions traversing a list

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 well that the lambda expression above will return a True or False. The question is: Is it like with a Lambda I load a variable string with the value of the expression? That is, transacaoPV gets the value of: frmPVListaProcessos.aspx These lambdas 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");
    
asked by anonymous 10.03.2015 / 13:15

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 default of the string (and any other non-primitive class) is null, so the variable transacaoPV will be null if it does not find the string in question.

I've just looked at the text of the title of the question and although I understand what you mean, it's a bit confusing.

    
10.03.2015 / 13:24