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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:17:23+00:00 2026-05-31T13:17:23+00:00

I Have a entity with a property referencing other entity (ReferenceEntity in examples). With

  • 0

I Have a entity with a property referencing other entity (ReferenceEntity in examples).

With HQL i can do this:

select e.ReferenceEntity from Entity e where e.Id = :entityId

NHibernate will give me the ReferenceEntity instance without lazy.

With query over im trying do this:

Session.QueryOver<Entity>()
.Where(e => e.Id == entityId)
.Select(e => e.ReferenceEntity)
.SingleOrDefault<ReferenceEntity>()

With QueryOver Nhibernate is giving me the ReferenceEntity but lazy.

I wanna get ReferenceEntity with eager loading using queryover like i do with hql.

Thanks

  • 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-31T13:17:24+00:00Added an answer on May 31, 2026 at 1:17 pm

    Suggestion #1

    You could do a little bit of LINQ manipulation after you execute the query to grab the data you want.

    var result = Session.QueryOver<Entity>()
        .Where(e => e.Id == entityId)        // Filter,
        .Fetch(e => e.ReferenceEntity).Eager // join the desired data into the query,
        .List()                              // execute database query,
        .Select(e => e.ReferenceEntity)      // then grab the desired data in-memory with LINQ.
        .SingleOrDefault();
    Console.WriteLine("Name = " + result.Name);
    

    It’s simple and gets the job done.

    In my test, it resulted in a single query. Here’s the output:

    SELECT
        this_.Id as Id0_1_, this_.Name as Name0_1_, this_.ReferenceEntity_id as Referenc3_0_1_,
        q5379349_r2_.Id as Id1_0_, q5379349_r2_.Name as Name1_0_
    FROM
        [Entity] this_
        left outer join [ReferenceEntity] q5379349_r2_
            on this_.ReferenceEntity_id=q5379349_r2_.Id
    WHERE this_.Id = @p0;
    

    Suggestion #2

    Another approach would be to use an EXISTS subquery, which would be slightly more complex, but would return the right result the first time without any need for post-database manipulation:

    ReferenceEntity alias = null;
    var result = Session.QueryOver(() => alias)
        .WithSubquery.WhereExists(QueryOver.Of<Entity>()
            .Where(e => e.Id == entityId)                 // Filtered,
            .Where(e => e.ReferenceEntity.Id == alias.Id) // correlated,
            .Select(e => e.Id))                           // and projected (EXISTS requires a projection).
        .SingleOrDefault();
    Console.WriteLine("Name = " + result.Name);
    

    Tested – results in single query:

    SELECT this_.Id as Id1_0_, this_.Name as Name1_0_
    FROM [ReferenceEntity] this_
    WHERE exists (
        SELECT this_0_.Id as y0_
        FROM [Entity] this_0_
        WHERE this_0_.Id = @p0 and this_0_.ReferenceEntity_id = this_.Id);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have an entity derived from db.Expando I can write Dynamic property by
I have this DependencyProperty which holds an entity with a property that is a
I have an entity collection tied to another entity (navigation property) I will always
I would like to change the entity property from String to long. I have
In ASP.NET MVC 4 Beta, I have an entity with this property: [DisplayFormat(DataFormatString =
I have an entity class that has a property with an underlying db column
I have an entity with a ModifiedDateTime property which I want to be updated
I have two Entity classes: Order and OrderItem . Order contains a navigation property
I have a collection of custom entity objects one property of which is an
I have a property of type uint on my entity. Something like: public class

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.