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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T22:09:32+00:00 2026-05-21T22:09:32+00:00

I am using Linq2NHibernate to fetch a bunch of data from a table in

  • 0

I am using Linq2NHibernate to fetch a bunch of data from a table in several queries. Although the table contains 10 columns, I am only using 3 of them in my query, and I don’t need other properties afterwards. Generated SQL nevertheless loads (as expected) all properties on each query.

What is the recommended way to optimize the query?

Does it make sense to make lighter entity variants to be used for optimized queries? I.e. to create an entity containing only 2 properties, and map it to the same table? If the actual entity inherits from the lighter entity, it shouldn’t make too much mess, and NHibernate would end up fetching only the smaller subset of data.

Is this common practice, or is there a better way to optimize queries? I would prefer to stick to LINQ, since it is used throughout the project right now.

[Edit]

As Mike answered below, the solution is pretty obvious: actual projected DTOs will dictate the necessary properties to fetch. So I will provide an example, just to make it clearer:

In other words, this query will load all properties:

/* fetch actual entities */
var results = session
     .Query<Food>()
     .Where(p => p.Proteins < 100 && p.Fats < 50);

/* ... results in: */
SELECT Id, Proteins, Fats, Carbs, Name, Whatever
FROM [Food] WHERE Proteins < 100 AND Fats < 50

While this one will generate the query to fetch only the projected properties:

/* fetch light DTOs */
var results = session
     .Query<Food>()
     .Where(p => p.Proteins < 100 && p.Fats < 50)
     .Select(p => new 
     {
        Proteins = p.Proteins,
        Fats = p.Fats        
     });

/* generated sql: */
SELECT Proteins, Fats
FROM [Food] WHERE Proteins < 100 AND Fats < 50
  • 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-21T22:09:34+00:00Added an answer on May 21, 2026 at 10:09 pm

    Since you haven’t posted your actual query it’s difficult to see what the problem is. Typically, you would populate a data transfer object (DTO) with only the fields you require. That will fix one issue. As for your LINQ query including every field in your domain entity you have to use projections. Projections will tell NHibernate to only fetch those excluding the rest of your domain. Note that I don’t query NHibernate with LINQ but the concepts are the same.

    As an aside I do agree with Matías Fidemraizer that you should consider using the new QueryOver API.

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

Sidebar

Related Questions

Using Java,I have to fetch multiple sets of values from an XML file to
Using a populated Table Type as the source for a TSQL-Merge. I want to
Using ASIHTTPRequest, I downloaded a zip file containing a folder with several audio files.
Using Microsoft SQL Server 2005, is there any way to see when a table
I'm working with NHibernate 2 in a .Net project and I'm using the Linq2NHibernate
I have a linq query that is passed into NHibernate which Linq2NHibernate will parse
First of all, I'm using Fluent NHibernate with LinqToNHibernate. I've got a query to
Using mercurial, I've run into an odd problem where a line from one committer
Using Android TelephonyManager an application can obtain the state of data activity over the
Using Yii, I want to delete all the rows that are not from today.

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.