Lambda expressions - set the value of one property in a collection of objects based on the value of
Posted
by Michael Rut
on Stack Overflow
See other posts from Stack Overflow
or by Michael Rut
Published on 2010-05-26T19:05:04Z
Indexed on
2010/05/26
19:11 UTC
Read the original article
Hit count: 236
I'm new to lambda expressions and looking to leverage the syntax to set the value of one property in a collection based on another value in a collection
Typically I would do a loop:
class item{
public string name {get;set;}
public string value {get;set;}
}
class business
{
item item1 = new item(name="name1");
item item2 = new item(name="name2");
item item3 = new item(name="name3");
Collection<item> items = new Collection() {item1,item2,item3};
//This is what I want to simplify
for( int i = 0; i < items.count; i++)
{
if(items[i].item.name == "name2")
{
//set the value
items[i].item.value = "value2";
}
}
}
© Stack Overflow or respective owner