如何设置列表对象的多个值,我正在做以下但失败.
objFreecusatomization .AllCustomizationButtonList .Where(p => p.CategoryID == btnObj.CategoryID && p.IsSelected == true && p.ID == btnObj.ID) .ToList() .ForEach(x => x.BtnColor = Color.Red.ToString(),);
在逗号后我想要设置另一个值.
应该是什么表达,虽然我只有一个记录相关.
解决方法
好吧,我个人也不会那样编写代码 – 但你可以使用
statement lambda:
A statement lambda resembles an expression lambda except that the statement(s) is enclosed in braces
The body of a statement lambda can consist of any number of statements; however,in practice there are typically no more than two or three.
所以ForEach调用看起来像这样:
.ForEach(x => { x.BtnColor = Color.Red.ToString(); x.OtherColor = Color.Blue.ToString(); });
我会写一个foreach循环,但是:
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(); }