Reputation: 6027
In a situation where an EntitySet
is being queried (in, say, a many-to-many relationship), is it possible to access the parent object?
e.g
Thing thing = db.Things.First();
Widget widget = thing.Widgets.First();
// Let's assume that Widgets can have many things as well
// (i.e. widget.Things is possible)
widget.ParentThing // would return the same instance of thing used above
Is that possible?
Upvotes: 2
Views: 114
Reputation: 152511
is it possible to access the parent object
With a many-to-many there really isn't a "parent" - there are multiple related objects. A many-to-many is usually modeled with navigation properties:
public class Thing
{
public int ID {get; set;}
public virtual IEnumerable<Widget> Widgets {get; set;}
}
public class Widget
{
public int ID {get; set;}
public virtual IEnumerable<Thing> Things {get; set;}
}
If your model doesn't have such properties then an alternative is to go back to the context (or back to the db if you don't have the context anymore):
var relatedThings = db.Things
.Where(t => t.Widgets.Any(w => ID == widget.ID));
Upvotes: 4