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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:48:24+00:00 2026-06-12T18:48:24+00:00

I have two classes (just recreating the problem): public class User { public virtual

  • 0

I have two classes (just recreating the problem):

public class User
{
    public virtual int Id { get; set; }
    public virtual string FirstName { get; set; }
    public virtual string LastName { get; set; }
    public virtual IList<OrgUnitMembership> OrgUnitMemberships { get; set; }
}

public class OrgUnitMembership
{
    public virtual int UserId { get; set; }
    public virtual int OrgUnitId { get; set; }
    public virtual DateTime JoinDate { get; set; }
    public virtual DateTime LeaveDate { get; set; }
}

There’s a Fluent NHibernate map for both, of course:

    public class UserMapping : ClassMap<User>
    {
        public UserMapping()
        {
            Table("Users");

            Id(e => e.Id).GeneratedBy.Identity();
            Map(e => e.FirstName);
            Map(e => e.LastName);

            HasMany(x => x.OrgUnitMemberships)
.KeyColumn(TypeReflector<OrgUnitMembership>
.GetPropertyName(p => p.UserId))).ReadOnly().Inverse();
        }
    }
    public class OrgUnitMembershipMapping : ClassMap<OrgUnitMembership>
    {
        public OrgUnitMembershipMapping()
        {
            Table("OrgUnitMembership");

            CompositeId()
                .KeyProperty(x=>x.UserId)
                .KeyProperty(x=>x.OrgUnitId);

            Map(x => x.JoinDate);
            Map(x => x.LeaveDate);

            References(oum => oum.OrgUnit)
.Column(TypeReflector<OrgUnitMembership>
.GetPropertyName(oum => oum.OrgUnitId)).ReadOnly();
            References(oum => oum.User)
.Column(TypeReflector<OrgUnitMembership>
.GetPropertyName(oum => oum.UserId)).ReadOnly();
        }
    }

What I want to do is to retrieve some users based on criteria, but I would like to combine all columns from the Users table with some columns from the OrgUnitMemberships table, analogous to a SQL query:

select u.*, m.JoinDate, m.LeaveDate
from Users u inner join OrgUnitMemberships m on u.Id = m.UserId
where m.OrgUnitId = :ouid

I am totally lost, I tried many different options. Using a plain SQL query almost works, but because there are some nullable enums in the User class AliasToBean fails to transform, otherwise wrapping a SQL query would work like this:

return
    Session
        .CreateSQLQuery(sql)
        .SetParameter("ouid", orgUnitId)
        .SetResultTransformer(Transformers.AliasToBean<UserDTO>())
        .List<UserDTO>()

I tried the code below as a test (a few different variants), but I’m not sure what I’m doing. It works partially, I get instances of UserDTO back, the properties coming from OrgUnitMembership (dates) are filled, but all properties from User are null:

User user = null;
                OrgUnitMembership membership = null;
                UserDTO dto = null;
                var users = Session.QueryOver(() => user)
                    .SelectList(list => list
                        .Select(() => user.Id)
                        .Select(() => user.FirstName)
                        .Select(() => user.LastName))
                    .JoinAlias(u => u.OrgUnitMemberships, () => membership)
                    //.JoinQueryOver<OrgUnitMembership>(u => u.OrgUnitMemberships)
                    .SelectList(list => list
                        .Select(() => membership.JoinDate).WithAlias(() => dto.JoinDate)
                        .Select(() => membership.LeaveDate).WithAlias(() => dto.LeaveDate))
                    .TransformUsing(Transformers.AliasToBean<UserDTO>())
                    .List<UserDTO>();
  • 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-12T18:48:26+00:00Added an answer on June 12, 2026 at 6:48 pm

    so close.. just join your 2 SelectList methods – your aliases will ensure that NHibernate gets the right property from the right entity:

    User user = null;
    OrgUnitMembership membership = null;
    UserDTO dto = null;
    var users = Session.QueryOver(() => user)
        .JoinAlias(u => u.OrgUnitMemberships, () => membership)
        //.JoinQueryOver<OrgUnitMembership>(u => u.OrgUnitMemberships)
        .SelectList(list => list
            .Select(() => user.Id)
            .Select(() => user.FirstName)
            .Select(() => user.LastName))
            .Select(() => membership.JoinDate).WithAlias(() => dto.JoinDate)
            .Select(() => membership.LeaveDate).WithAlias(() => dto.LeaveDate))
        .TransformUsing(Transformers.AliasToBean<UserDTO>())
        .List<UserDTO>();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes: class Car<T> { public string color { get; set; }
I have two classes: public class PageMeta { public PageMeta() { } public string
I have two classes set up as follows: class Point { protected: double coords[3];
I have two classes, named Post and Question. Question is defined as: public class
OK, I've tried but I just don't get it. I have two classes logger
I have two classes in short here they are: public final class ServerMain {
A very simple example. I have two classes: public class BaseControl : UserControl {
Ok I have two modules, each containing a class, the problem is their classes
I have two classes Class A and ClassB: static class ClassA { static string
I have two classes: class A { public: /** Brief description * Grand description

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.