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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:18:27+00:00 2026-05-16T23:18:27+00:00

I have a problem with nHibernate projections. I don’t even know if what I’m

  • 0

I have a problem with nHibernate projections. I don’t even know if what I’m attempting can be done but if any nHibernate experts can help me I’d be grateful.

I’m using nHibernate criteria to query a database and I’m returning the result projected to a different (slimmed down) object.

I get a list of returns like

Id    CompanyId  Description  
1        1          Desc1  
1        2          Desc1  
1        3          Desc1  
2        1          Desc2  
2        3          Desc2  
3        1          Desc3  
3        2          Desc3  
3        3          Desc3  

when I use this object

int Id  
int CompanyId  
string Description

What I’m looking for is to get something more like

Id CompanyId Description  
1  [1, 2, 3] Description  
2  [1, 3] Description  
3  [1, 2, 3] Description  

From an object like this

int id  
List`<int`> companyId  
string description  

The current code I have is similar to

 result = session.CreateCriteria<Object>()
           .Add(Restrictions.Eq("SiteId", 616))
           .SetProjection(Projections.Distinct(Projections.ProjectionList()
           .Add(Projections.Property("Id"), "Id")
           .Add(Projections.Property("CompanyId"), "CompanyId")
           .Add(Projections.Property("Description"), "Description")
           .SetResultTransformer(Transformers.AliasToBean<ObjectReduced>()).List<ObjectReduced>();

So I’d like to know if there is anything here I can do to achieve this, or even if this is completely the wrong approach and there is something better.

  • 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-16T23:18:28+00:00Added an answer on May 16, 2026 at 11:18 pm

    You can’t do that directly in NHibernate. The best way to achieve this is to get the result set as you are currently doing and then populate the reduced object with the CompanyIds. Something like:

    var temp = session.CreateCriteria<Object>()
               .Add(Restrictions.Eq("SiteId", 616))
               .SetProjection(Projections.Distinct(Projections.ProjectionList()
               .Add(Projections.Property("Id"), "Id")
               .Add(Projections.Property("CompanyId"), "CompanyId")
               .Add(Projections.Property("Description"), "Description")
               .SetResultTransformer(Transformers.AliasToBean<ObjectReduced>())
               .List<ObjectReduced>();
    var groups = temp.GroupBy(x => x.Id);
    var result = new List<ObjectReduced>(groups.Count());
    foreach (var member in groups)
    {
        var first = member.First();
        var companyIds = member.Select(x => x.CompanyId);
        foreach (var companyId in companyIds)
        {
            first.CompanyIds.Add(companyId);
        }
        result.Add(first);
    }
    return result;
    

    There’s probably a LINQier way to do this but this is how I solved a similar problem.

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

Sidebar

Related Questions

I have a very weird problem: sometimes when I call nHibernate update to an
I have problem compilin this code..can anyone tell whats wrong with the syntax CREATE
I do not have problem as such but I am quite new to Ruby.
I have a problem with: NHibernate.Cfg.Configuration.SetProperties() Not accepting the IDictionary: NHibernateConfigHandler I get the
I have a problem with nhibernate when I try to insert an object with
I have a problem with the DefaultUpdateEventListener in NHibernate. I will update 2 objects
Question: I have an annoying problem with nhibernate. The problem is I cannot get
I have a problem with uninitialized proxies in nhibernate The Domain Model Let's say
I have problem in some JavaScript that I am writing where the Switch statement
I have problem with return statment >.< I want to store all magazine names

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.