Continuing from the Q&As dealing with looping through an object's properties (Using LINQ to loop through inner class properties in outer class collection), is it possible to populate a dictionary:
Dictionary<ComplexKey,IEnumerable<double>> answer;
For example,
- answer[1,1,1,"MeasurementA"] = {2.0, 2.1}
- answer[1,1,1,"MeasurementB"] = {3.0, 3.1}
- answer[1,1,1,"MeasurementC"] = {4.0, 4.1}
- answer[1,1,2,"MeasurementA"] = {5.0, 5.1}
Given the structure:
class MainClass {
List<Class1> list
}
class Class1 {
// a number of fields including...
int PropertyA { get; set; }
int PropertyB { get; set; }
Dictionary<int, Class2> dict { get; set; }
}
class Class2 {
// a number of fields all of type double...
double MeasurementA { get; set; }
double MeasurementB { get; set; }
double MeasurementC { get; set; }
}
struct ComplexKey {
public int class1PropA;
public int class1PropB;
public int class1DictKey;
public string class2PropName;
}
Given data:
MainClass mainClass = new MainClass();
mainClass.list = new List<Class1>() {
new Class1() {
PropertyA = 1,
PropertyB = 1,
dict = new Dictionary<int,Class2>() {
{ 1, new Class2() { MeasurementA = 2.0, MeasurementB = 3.0, MeasurementC = 4.0 }},
{ 2, new Class2() { MeasurementA = 5.0, MeasurementB = 6.0, MeasurementC = 7.0 }}
}
},
new Class1() {
PropertyA = 1,
PropertyB = 1,
dict = new Dictionary<int,Class2>() {
{ 1, new Class2() { MeasurementA = 2.1, MeasurementB = 3.1, MeasurementC = 4.1 }},
{ 2, new Class2() { MeasurementA = 5.1, MeasurementB = 6.1, MeasurementC = 7.1 }}
}
}
};
Noting in this example, Class1.PropertyA and Class1.PropertyB are consistently set to "1" for brevity. (This is not the case in the real world data set).
I believe populating the Dictionary "answer" would require, grouping the mainclass.list by PropertyA, PropertyB, and dict.Key before accessing the properties and values within dict.Values (i.e. instances of Class2).
I thought LINQ was the answer but have been stuck for many weeks. Any direction would be appreciated.
Thanks & regards Shannon