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 8277931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:50:18+00:00 2026-06-08T08:50:18+00:00

I have a simple categories management system. In my auto-generated EF entities I have

  • 0

I have a simple categories management system.

In my auto-generated EF entities I have two classes of relevance:

class Category {
    public EntityCollection<Member> Members { get; }
}
class Member {
    public Category ParentCategory { get; }
}

I have a business rule that prevents the deletion of a category if it has any members. I was hoping to accomplish that by doing this:

if( category.Members.Count() > 0 ) // then disallow deletion

However this call always returns zero.

It appears that I need to actually load the collection first, which incurrs a DB hit:

if( !category.Members.IsLoaded ) category.Members.Load();
if( category.Members.Count() > 0 ) // then disallow deletion. This works.

I was hoping that the EF would be smart enough to convert the initial .Count() call into the SQL SELECT COUNT(*) FROM Members WHERE Members.CategoryId = @parentCategoryId instead of actually loading Members entities and iterating through them in-memory.

Is there a way to do that in EF, or do I need to define a sproc or custom SQL to achieve this?

  • 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-08T08:50:21+00:00Added an answer on June 8, 2026 at 8:50 am

    Try

    if ( category.Members.CreateSourceQuery().Count() > 0 )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple model with news and categories: class Category(models.Model): name = models.CharField()
I have the following simple textbook classes defined: public class Product { public int
I have a very simple model with two objects: Name and Category. One Name
I have a simple product list, the products fall into two distinct categories and
I have a simple category model: class Category(models.Model): name = models.CharField(max_length=200) slug = models.SlugField()
I'm looking for a simple modelformset example - I have a model class Category(BaseModel):
I have this simple situation: @Entity public class Customer{ @ManyToMany(fetch=FetchType.EAGER) @Cascade(CascadeType.SAVE_UPDATE) private List<Product> products=new
I have two tables ITEMS : id, name CATEGORIES: items_id, category I need to
I have a simple hierarchy that I'm trying to build. class Category < ActiveRecord::Base
I have a simple MySQL table thats contains a list of categories, level is

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.