How to set multiple values ​​in a list using lambda expression?

How to set multiple values ​​of a list object, I do the following, but it doesn't work.

objFreecusatomization .AllCustomizationButtonList .Where(p => p.CategoryID == btnObj.CategoryID && p.IsSelected == true && p.ID == btnObj.ID) .ToList() .ForEach(x => x.BtnColor = Color.Red.ToString(), ); 

At the end after the decimal point, I want to set a different value. What should be the expression, although I have only one entry.

+6
source share
1 answer

It’s good that I won’t write code this way at all, but you can just use the lambda operator:

The lambda operator resembles the expression lambda, except that the operator is enclosed in braces

The body of a lambda statement can consist of any number of statements; however, in practice, usually no more than two or three.

Thus, the ForEach call will look like this:

 .ForEach(x => { x.BtnColor = Color.Red.ToString(); x.OtherColor = Color.Blue.ToString(); }); 

I would write a ForEach loop instead:

 var itemsToChange = objFreecusatomization.AllCustomizationButtonList .Where(p => p.CategoryID == btnObj.CategoryID && p.IsSelected && p.ID == btnObj.ID); foreach (var item in itemsToChange) { item.BtnColor = Color.Red.ToString(); item.OtherColor = Color.Blue.ToString(); } 

(You can embed the query in a ForEach statement, but I personally find the above approach using a separate local variable cleaner.)

+12
source

Source: https://habr.com/ru/post/952357/


All Articles