Filling Label with Linq using Tolist()?

Asked

Viewed 82 times

2

I have a table with the following schematics:

GarantiaImovel | imovelID | GarantiasTipos
12             |    3     | Caução2
12             |    4     | Depósito
12             |    5     | Caução2
12             |    5     | Fiador
12             |    5     | Seguro Fiança

So I have the following Query:

var garantias = (from p in db.T_GarantiasLocaticias
                         where p.imovelID == 5
                         select p.GarantiasTipos).ToList();

My intention in this Query is to return the three rows in the table above with the column GaantiasTipo, but me using the foreach and it only returns me the last line as I can return in the following structure:

foreach (var garantia in garantias)
{
     lblGarantiaLocaticias.Text = garantia + ", ";
}

How to proceed?

  • You could just use one +=. Thus: lblGarantiaLocaticias.Text += garantia + ", ";. The foreach will be produced in any way, either explicitly or in the string.Join.

2 answers

1


If you don’t need to make one foreach just use String.Join with the return of that expression , example:

var garantias = (from p in db.T_GarantiasLocaticias
                  where p.imovelID == 5
                  select p.GarantiasTipos)
               .ToList();

lblGarantiaLocaticias.Text = String.Join(", ", garantias);

Online Example

Observing: can even use a for or foreach or even a while, but, there is no need already seen that String.Join already makes it so transparent. Another observing is about what was happening, because it lacked accumulate the values already obtained in the interactions and that in the foreach of your question did not, in case it would only put a sign of more (+) before the same (=) (representing an addition allocation operator) and an adjustment so that in the last element there was no comma (,) in this way:

lblGarantiaLocaticias.Text = "";
foreach (var garantia in garantias)
{
     if (lblGarantiaLocaticias.Text != "") 
         lblGarantiaLocaticias.Text += ", ";
     lblGarantiaLocaticias.Text += garantia;
}

References:

1

You can use the Aggregate(...) (alias reduce):

var garantias = (from p in db.T_GarantiasLocaticias
                 where p.imovelID == 5
                 select p.GarantiasTipos)
                .ToList()
                .Aggregate(string.Empty, (actual, novo) => 
                {
                    // caso a string concatenada nao esteja vazia,
                    // adiciona o separador
                    if(!string.IsNullOrEmpty(actual)) {
                        actual += ","
                    }

                    return actual + novo;
                });

You can see an example of Aggregate here.

  • Looking at it like this, it seems that it would be a good option, but in addition to the coding errors and lack of coding it ends up doing the same way as mine did in the first solution, so, "Se não quiser percorrer a colecção duas vezes, pode usar" is not a valid argument.

  • @Virgilionovic depends, if the OP uses the objects brought to something else, there is nothing wrong with using the string. Join. If you just want to build the string, avoid materializing the collection with Tolist and then scroll through it again with the string.Join(...). Even if you do not consider the argument valid, Aggregate remains a viable alternative. And thank you for indicating the errors ;)

  • The Aggregate will only work after the materialization of the ORM layer (that is, in Linq for Objects) used, IE, will give in the same, did not say that this solution is not feasible it is feasible yes, just said that the argument used does not check to use the solution.

Browser other questions tagged

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