Assume two classes, simplified
Class Room {
String Name {get; set;}
String Type {get;set;}
List<Employee> Employees {get; set;}
}
Class Employee {
Int64 ID {get;set;}
Room Parent {get; set;}
}
I get XML data from a database and use LINQ to read this XML into objects:
List<Room> rooms = new List<Room>();
rooms =
(
from r in XDocument.Load(reader).Root.Elements("Room") select new Room {
Name = r.Attribute("name").Value,
Type = r.Attribute("type").Value,
Employees =
(
from e in r.Elements("Employee") select new Employee {
ID = (Int64)e.Attribute("hrid"),
Parent = ?????
}
).ToList()
}
).ToList();
Notice the ?????? up there? That’s the question. How can I create a reference to the parent Room within the Employee?
It’s not the best practice but you can set null for parent or just discard first. And after created your list, you can iterate through rooms and set employee parents.