An interesting problem, and maybe there is a way to do this by setting the properties in the query rule, but I think the other possibility is to move part of this logic to the constructor inside State. Consider the following code example
class State { public int Id { get; set; } public List<City> Children { get; set; } public State(int id, IEnumerable<City> childrenCities) { this.Id = id; this.Children = childrenCities.ToList(); foreach (City city in this.Children) city.ParentState = this; } }
This State class has a constructor that accepts enumerated City objects. It then iterates over the objects and sets the ParentState property.
Then, instead of setting properties with a query expression, you call the constructor instead.
// not LINQ-to-XML, just an example var states = from i in Enumerable.Range(0, 10) select new State( i, from j in Enumerable.Range(0, 5) select new City { Id = j } );
Anthony pegram
source share