I Have a class that has a non-primitive property.
I need to update some child property of that property of parent class.
public class Parent
{
public string Abc { get; set; }
public Childclass Pos { get; set; }
}
public class Childclass
{
public string Value { get; set; }
}
List<Parent> parents = new List<Parent>()
Parent p1 = new Parent();
p1.Pos.Value = "1";
parents.Add(p1);
Parent p2 = new Parent();
p2.Pos.Value = "2";
parents.Add(p2);
Now I need to update Pos
in parentswhere Pos.Value == "2"
?
List<Parent> parents = new List<Parent>();
Parent p1 = new Parent();
p1.Pos = new Childclass() { Value = "1" };
parents.Add(p1);
Parent p2 = new Parent();
p2.Pos = new Childclass() { Value = "2" };
parents.Add(p2);
If you need to update every items :
foreach (Parent parent in parents.Where(e => e.Pos.Value.Equals("2")))
parent.Pos.Value = "new value";
If you need to update the first item only :
parents.FirstOrDefault(e => e.Pos.Value.Equals("2")).Pos.Value = "new value";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With