Load a single related object
I need to load a single related object from an navigation property (ICollection) to send to my MVC View.
This can save me from unnecessary db access and load.
I've found this article about loading re开发者_运维问答lated objects but didn't figure out how to load one single related object from the list.
To be short, I need the object and inside its navigation property one single related object.
How to achive this?
You can't do this using Include. Include will bring back all related entities for the navigation property. You can either write two separate queries or write a join in your query.
Writing two queries:
var princess = context.Princesses.Find(id);
var unicorns = context.Unicorns.Where(u => u.PrincessId == id && u.UnicornName == "Blinky");
princess.Unicorns = unicorns.ToList();
精彩评论