开发者

Entity Framework 4 (Assocations configured with filters)

开发者 https://www.devze.com 2023-04-11 10:46 出处:网络
I have a user entity that contains a collection of survey entities. i would like the 开发者_StackOverflowassocation to include a filter on the relationship, such as \'IsCompleted\', so whenever i eage

I have a user entity that contains a collection of survey entities. i would like the 开发者_StackOverflowassocation to include a filter on the relationship, such as 'IsCompleted', so whenever i eager load (or lazy load for that matter) the collection, this filtering happens.

Is this something we have control over?

thanks!


If you are using a DB back-end that supports views, you might consider using the view as the source for the collection of survey entities. Leverage the power of the DB to do that filtering for you.


Loading of associations for an entity always just gets them all, whether because you used Include during the initial query, called Load after the fact, or lazy-loading caused it. The concept of the navigation property kind of assumes this behavior.

E.J. Brennan's answer would work well. If you're not concerned about loading all surveys behind the scenes (because of performance/memory reasons or something) then you might also consider creating a separate property via a partial class definition on your entity that returns the filtered list.

public partial class User
{
    public ICollection<Survey> CompletedSurveys
    {
        get { return Surveys.Where(s => s.IsCompleted); }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号