1

I have the following classes:

public class Class_A
{
    public string my_field { get; set; }
    public int id { get; set; } 
    // etc...
}

public class Class_B
{
    public string my_field { get; set; }
    public Class_A field_A { get; set; } 
    // etc...
}

public class Class_C
{
    public string my_field { get; set; }
    public Class_A field_A { get; set; } 
    // etc...
}

List<Class_A> list_A = new List<Class_A>(); //and then populate it
List<Class_B> list_B = new List<Class_B>(); //and then populate it
List<Class_C> list_C = new List<Class_C>(); //and then populate it

And then, when I update some element of list_A, for example

Class_A old_a, new_a;
old_a = list_A.Where("some condition").FirstOrDefault();
new_a = new Class_A();//and then initialize it
list_A[list_A.FindIndex(x => x.Equals(old_a))] = new_a;

I need, that all elements of list_B and list_C, which field Class_A equals old_a will be update to new_a.

1. What is the best way to do this? Now I have following variant, but I think, that it's could be better:

list_B.Where(x => x.Class_A.Equals(old_a)).ForEach(x => x.Class_A = new_a);

2. What is the best way to update all values, if I'll have this code?

public class Class_D
{
    public string my_field { get; set; }
    public List<Class_A> list_A { get; set; } 
    // etc...
}

List<Class_D> list_D = new List<Class_D>(); //and then populate it
Vlad i Slav
  • 61
  • 1
  • 8

1 Answers1

1

Have a look at ObservableCollection and Item PropertyChanged

You can make from your list_A an observable collection when can be received by Class_B and Class_C.

Aldert
  • 3,286
  • 1
  • 6
  • 18