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

The Archive Base Latest Questions

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

I have 3 NHibernate entities Division { Id (PK), Name } District { Id

  • 0

I have 3 NHibernate entities

Division
{
  Id (PK),
  Name
}

District
{
  Id (PK),
  Name
}

Doctor
{
  Id (PK),
  Name,
  Division,
  District
}

I want to get doctors filtering by the division and district.

I can write query by two way, say I have a user-selected Division and I want the doctors under this division.

First way:

var doctors = doctorRepository
    .FilterBy(x => x.Division.Id == selectedDivision.Id)
    .ToList();

Second Way:

var doctors = doctorRepository
    .FilterBy(x => x.Division == selectedDivision)
    .ToList();

My First question is, which is the best way considering the performance?

I checked the sql profiler for both queries.
For first query, the generated sql contains a left outer join with division table. I can’t understand why it needs to join with Division table as the DivisionId reside in the Doctor table. Will this extra join reduce the performance?

My Second question: if I want to query from the doctor repository using Division and District and both division and district can be null sometimes, in this case how should I write this query in NHibernate Linq?

  • 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-18T22:08:29+00:00Added an answer on June 18, 2026 at 10:08 pm

    For first query, the generated sql contains a left outer join with division table. I can’t understand why it needs to join with Division table as the DivisionId reside in the Doctor table. Will this extra join reduce the performance?

    It may not strictly need to, probably it’s just that this detail isn’t optimized by the NHibernate Linq provider. Unless your database engine realizes that it can be optimized away (look at the execution plan for the SQL), it will affect performance. How much is impossible to tell in the general case.

    My Second question: if I want to query from the doctor repository using Division and District and both division and district can be null sometimes, in this case how should I write this query in NHibernate Linq?

    Consider a query such as:

    var doctors = doctorRepository
        .FilterBy(x => x.Division.Id == selectedDivision.Id)
        .ToList();
    

    When executed by Linq2NH it will be converted to SQL. If x.Division is NULL nothing bad will happen, because the expression will never be dereferenced in .NET. I’m uncertain if NHibernate will try to dereference selectedDivision (causing NRE) or if it’s smart enough to convert selectedDivision.Id into NULL if selectedDivisionis NULL. Try it! Otherwise, a safer way is to write:

    int? divId = selectedDivision != null ? selectedDivision.Id : (int?)null;
    var doctors = doctorRepository
        .FilterBy(x => x.Division.Id == divId)
        .ToList();
    

    NHibernate is smart enough to generate IS NULL in the SQL if divId happens to be null.

    Null as indication to avoid filter

    I typically use a pattern such as:

    var queryable = GetMyBaseQueryable<Doctor>();
    
    if (divisionFilter != null)
        queryable = queryable.Where(x => x.Division.Id == divisionFilter.Id)
    
    if (districtIdFilter != null)    // districtIdFilter is some collection
        queryable = queryable.Where(x => districtIdFilter.Contains(x.District.Id))
    
    var result = queryable.ToList();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an nHibernate query issue that looks quite straight forward, but I can't
I have a very silly doubt in NHibernate. There are two or three entities
I have two entities like public class Person { public int PersonId { get;
I have two entities, EntityA and EntityB. EntityB can optionally be related to EntityA,
I'm using the current Linq provider for NHibernate (version 2.1). I have two entities:
I have got two kind of entities, comments and tasks. Both types can be
I have two entities with many-to-many relationship defined on them. <set name=TreasuryCodes table=ServiceProviderAccountTreasuryCode lazy=true
I'm doing some tests using NHibernate and I currently have two mapped entities, Orders
I have 2 NHibernate entities (tables) that are in a one to many relationship.
I have a complex JPA/Hibernate problem. I have two entities A and B. A

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.