假设两个类,简化
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;}
}
我从数据库中获取 XML 数据并使用 LINQ 将此 XML 读入对象:
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();
注意?????? 在上面?这就是问题所在。如何在 Employee 中创建对父 Room 的引用?