Get Item From List<T> and update a property on that Item
I have a List of type Person that I would like to update DisplayValue based on DisplayName. How can I accomplish this?
public class Person
{
public string DisplayName { get; set; }
public string D开发者_JAVA百科isplayValue { get; set; }
... other properties
}
Your question is unclear, but I think this is what you're after.
List<Person> persons;
var person = persons.Find(p => p.DisplayName == "Fred");
person.DisplayValue = "Flinstone";
var list = ... List<Person> from somewhere.
foreach(var person in list){
person.DisplayValue = person.DisplayName;
}
create a private variable, explicitly define the getter and setter, and modify Display value accordingly in the setter.
public class Person
{
private string displayName;
public string DisplayName
{
get { return displayName; }
set
{
displayName = value;
DisplayValue = value + " is a value";
}
}
public string DisplayValue { get; set; }
//... other properties
}
精彩评论