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

  • SEARCH
  • Home
  • 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 8288687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:19:37+00:00 2026-06-08T12:19:37+00:00

I want to limit model that are returned by a navigational property. For example,

  • 0

I want to limit model that are returned by a navigational property. For example, I am using an AuditInfo model to log the activity of a model. Once a model is deleted the DeletedBy and Deleted attributes are set. However since nothing is ever really “deleted” from the database, these models will still be populated in navigational properties referenced by other models.

AuditInfo Class

public class AuditInfo
{
    [Key]
    public int AuditInfoID { get; set; }

    //Other attributes

    public string DeletedBy { get; set; }

    public DateTime? Deleted { get; set; }
}

Class that has a navigational properties

public class BlogPost
{
    //Other attributes

    //Only return Comment where Comment.AuditInfo.Deleted is NULL
    public virtual IList<Comment> Comments { get; set; }
}

Class that is being audited

public class Comment
{
    //Other attributes

    public int AuditInfoID { get; set; }
}

How would I set up a constrain so that only the non-deleted comments (Comment.AuditInfo.Deleted is NULL) from a BlogPost.Comments?

  • 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-08T12:19:38+00:00Added an answer on June 8, 2026 at 12:19 pm

    (I assume you are using EF Code-First, because of the [Key] attribute.)

    There are different ways to load navigation properties and related entities and you can apply a filter for some of these ways but not for all:

    • Lazy loading:

      Your navigation property has to be virtual so that lazy loading works at all:

      public virtual IList<Comment> Comments { get; set; }
      

      Loading the parent:

      var blogPost = context.BlogPosts.Find(1);
      foreach (var comment in blogPost.Comments) // lazy loading triggered here
      {
      }
      

      You can’t apply a filter here. Lazy loading will always load all comments of the given blog post.

    • Eager loading:

      var blogPost = context.BlogPosts.Include(b => b.Comments)
          .SingleOrDefault(b => b.Id == 1);
      

      You can’t apply a filter in Include. Eager loading will always load all comments of the given blog post.

    • Explicit loading:

      Loading the parent:

      var blogPost = context.BlogPosts.Find(1);
      

      You can apply a filter when you load the comments now:

      context.Entry(blogPost).Collection(b => b.Comments).Query()
          .Where(c => !c.AuditInfo.Deleted.HasValue)
          .Load();
      
    • Projection:

      You can apply a filter in the projected properties:

      var blogPost = context.BlogPosts
          .Where(b => b.Id == 1)
          .Select(b => new
          {
              BlogPost = b,
              Comments = b.Comments.Where(c => !c.AuditInfo.Deleted.HasValue)
          })
          .SingleOrDefault();
      

    It is not possible to apply some kind of global filter policy in the model definition so that this filter gets applied for all methods above automatically and without specifying it explicitly in the explicit loading and in the projection example. (I think you have such a global model definition in mind, but that’s not possible.)

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

Sidebar

Related Questions

I have a model class that has an AreDuesPaid property that I want only
I want to limit my SWF file so that it cannot be stolen and
I have a Model with data that I want to use to populate a
I have a page model that I want to find by a handle that
I want to generate a password reset token for a User model that I
i just want limit a query by parameter inside a stored, but I get
I want to limit the size of a list in python 2.7 I have
I want to limit the use of phpsessid cookie only for secure connections during
I want to limit clients to an upper limit of number of calls to
I want to limit the what characters the user should be allowed to type

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.