Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8476999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:20:02+00:00 2026-06-10T18:20:02+00:00

I’m using entity framework with POCOs and the repository pattern and am wondering if

  • 0

I’m using entity framework with POCOs and the repository pattern and am wondering if there is any way to filter a child list lazy load. Example:

class Person
{
    public virtual Organisation organisation {set; get;}
}
class Organisation
{
    public virtual ICollection<Product> products {set; get;}
}
class Product
{
    public bool active {set; get;}
}

Currently I only have a person repository because I’m always starting from that point, so ideally I would like to do the following:

Person person = personRepo.GetById(Id);
var products = person.organisation.products;

And have it only load products where active = true from the database.

Is this possible and if so how?

EDIT My best guess would be either a filter can be added to the configuration of the entity. Or there might be a way to intercept/override the lazy load call and modify it. Obviously if I created an Organisation Repository I could manually load it as I please but I am trying to avoid that.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T18:20:04+00:00Added an answer on June 10, 2026 at 6:20 pm

    You can do what Mark Oreta and luksan suggest while keeping all the query logic within the repository.

    All you have to do is pass a Lazy<ICollection<Product>> into the organization constructor, and use the logic they provided. It will not evaluate until you access the value property of the lazy instance.

    UPDATE

    /*  
        First, here are your changes to the Organisation class: 
        Add a constructor dependency on the delegate to load the products to your 
        organization class.  You will create this object in the repository method 
        and assign it to the Person.Organization property 
    */
    public class Organisation
    {
        private readonly Lazy<ICollection<Product>> lazyProducts;
        public Organisation(Func<ICollection<Product>> loadProducts){
            this.lazyProducts = new Lazy<ICollection<Product>>(loadProducts);
        }
    
        //  The underlying lazy field will not invoke the load delegate until this property is accessed
        public virtual ICollection<Product> Products { get { return this.lazyProducts.Value; } }
    }
    

    Now, in your repository method, when you construct the Person object you will assign the Organisation property with an Organisation object containing the lazy loading field.

    So, without seeing your whole model, it will looks something like

     public Person GetById(int id){
        var person = context.People.Single(p => p.Id == id);
    
        /*  Now, I'm not sure about the cardinality of the person-organization or organisation
            product relationships, but let's assume you have some way to access the PK of the 
            organization record from the Person and that the Product has a reference to 
            its Organisation.  I may be misinterpreting your model, but hopefully you 
            will get the idea
         */
    
         var organisationId = /*  insert the aforementioned magic here */
    
         Func<ICollection<Product>> loadProducts = () => context.Products.Where(product => product.IsActive && product.OrganisationId == organisationId).ToList(); 
    
        person.Organisation = new Organisation( loadProducts );
    
        return person;
     }
    

    By using this approach, the query for the products will not be loaded until you access the Products property on the Organisationinstance, and you can keep all your logic in the repository. There’s a good chance that I made incorrect assumptions about your model (as the sample code is quite incomplete), but I think there is enough here for you to see how to use the pattern. Let me know if any of this is unclear.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I know there's a lot of other questions out there that deal with this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.