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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:08:53+00:00 2026-06-18T09:08:53+00:00

I am using nhibernate and I wrote a linq query and it is not

  • 0

I am using nhibernate and I wrote a linq query and it is not returning what I expect.

public ParentA()
{
    public virtual Id {get; set;}
    public virtual Name {get; set;}
    public virtual IList<ChildA> ChidrenA {get; set;}

   public ParentA()
   {
       ChidrenA = new List<ChildA>();      
   }
}

public ChildA()
{
    public virtual Id {get; set;}
    public virtual Name {get; set;}
    public virtual IList<ChildB> ChidrenB {get; set;}
    public virtual ParentA ParenteA {get; set;}
   public ChildA()
   {
       ChidrenB = new List<ChildB>();      
   }
}

public ChildB()
{
    public virtual Id {get; set;}
    public virtual Name {get; set;}
    public virtual ChildA {get; set;}
}

The above code is my domains. The fluent nhibernate would be very basic and nothing special going on so I have not included it.

The query I have is

base.unitOfWork.Session.Query<ParentA>()
  .Where(x => x.Id == parentAId)
  .FetchMany(x => x.ChildrenA)
  .ThenFetchMany(x => x.ChildrenB)
  .FirstOrDefault();

What I expected to happen

It will find 1 or 0 parent records. If it does find that one record it will eager load all ChildrenA and then all ChildrenB.

What is happening

It finds 1 or 0 parent records. It then only takes 1 or 0 record for ChildrenA and ChildrenB.

Why is only taking the first found record for ChildrenA and ChildrenB?

If I change FirstToDefault() to .toList() I get everything I expect but I find it pointless as there should only be one record with that parent record.

  • 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-18T09:08:54+00:00Added an answer on June 18, 2026 at 9:08 am

    Try moving .FirstOrDefault to directly after the .Where clause:

    .Where(x => x.Id == parentAId).FirstOrDefault()...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using nHibernate 3.2.0.4000. I wrote this query using nHibernate.Linq var entities = (from
i have the following code using Nhibernate.Linq var apps = Session.Linq<History>().OrderByDescending(r => r.LastUpdated).Take(50); Console.Write(apps.Count());
I have .NET 3.5 application wrote in C# using NHibernate. Before I has OracleClientDriver
I'm using Fluent NHibernate (the NH3 build - #694) and LINQ to connect to
I am using NHibernate (version 1.2.1) for the first time so I wrote a
Trying to write dynamic queries using the LINQ provider for NHibernate, but I am
i want to get data from sql server database in asp.net using NHibernate. what
Using NHibernate from C# and only HQL (not SQL) in a way that is
Let's say I have a SQL query I need to render using nHibernate. The
I write a big application by using of NHibernate ORM. Is using of full

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.