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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:45:02+00:00 2026-05-31T15:45:02+00:00

I have written the following query in Entity Framework and the values are different.

  • 0

I have written the following query in Entity Framework and the values are different.

var query = from p in db.Parents
            let children = p.Children
            let grandchildren = children.SelectMany(c => c.Grandchildren)
            select new 
            {
                Count1 = children.Count(c => !c.Grandchildren.Any()),
                Count2 = children.Count(c => !grandchildren.Any())
            };

The Count1 property returns what I expect, Count2 does not.

I am trying to return the number of Children that do not have any Grandchild objects. Count2 seems to return 0 in this case, but Count1 returns 1 with the following data set:

Parents

Id
------  
1

Child

Id    ParentId
--------------
1     1
2     1

Grandchild

Id    ChildId
-------------
1     1

I have two children, only one of them has any children. Why does the second query not seem to work the way I think it should?

My objects are as follows:

public class Parent {
    public int Id { get; set; }
    public virtual ICollection<Child> Children { get; set; }
}

public class Child { 
    public int Id { get; set; }
    public int ParentId { get; set; }
    public virtual ICollection<Grandchild> Grandchildren { get; set; }
}

public class Grandchild {
    public int Id { get; set; }
    public int ChildId { get; set; }
}
  • 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-05-31T15:45:04+00:00Added an answer on May 31, 2026 at 3:45 pm
    grandchildren.Any()
    

    is simply returning if there are any objects in your grandchildren collection. So it will always return true unless your SelectMany returns zero results.

    So, you will never get anything but Count2 = 0 because if there are any results !grandChildren.Any() will equal 0.

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

Sidebar

Related Questions

I am very new in LINQ I have written following query : var duplicate
I have written the following query: SELECT CLIENT.CLIENTNO, CLIENT.CNAME, TOTALS.TOTAL FROM CLIENT, (SELECT CLIENTNO,
I have written the following query. select distinct(table3.*), (select count(*) from table2 where table2.cus_id
I have written a DB2 query to do the following: Create a temp table
I have written the following: var pages=[[www.google.co.uk] This is the WWW. ,[www.yahoo.co.uk] This is
I have written the following query using the documentation at: Oracle Documentation to copy
Consider the following Query : var profilelst = ( from i in dbContext.ProspectProfiles where
I have following query written in HQL for Hibernate. ======================================================================== select new map(ret.retailerDesc as
I have written the following query: Private Sub Size_Sqft_BeforeUpdate(Cancel As Integer) Me!Size_Sqft = Nz(Me!Size_Sqft,
I have the following Entity: @Entity @Table @NamedQuery(name = Constants.FINDALLFINDERNAME, query = Constants.FINDALLQUERY) public

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.