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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:08:26+00:00 2026-06-14T19:08:26+00:00

I need to perform a join on the customer. Where address is primary and

  • 0

I need to perform a join on the customer. Where address is primary and populate PrimaryAddress1 and PrimaryCity field. Customer mapping already has hasmany relationship with address class but I do not want to fetch all addresses (performance issues).

Please help..

classes:

public class Customer
{
    public Customer()
    {
        Addressess = new List<Address>();
    }
   public virtual int CustomerID { get; set; }
    public virtual int? BranchID { get; set; }
    public virtual int? CustTypeID { get; set; }
    public virtual string CompanyName { get; set; }
    public virtual string Prefix { get; set; }
    public virtual string FirstName { get; set; }
    public virtual string MiddleName { get; set; }
    public virtual string LastName { get; set; }
    public virtual string PrimaryAddress1 { get; set; }
    public virtual string PrimaryCity { get; set; }
    public virtual List<Address> Addresses { get; set; }
}


public class Address
{
    public Address()
    {
    }
    public virtual int LocationID { get; set; }
    public virtual int? CustomerID { get; set; }
    public virtual string LocationName { get; set; }
    public virtual string Address1 { get; set; }
    public virtual string Address2 { get; set; }
    public virtual string Address3 { get; set; }
    public virtual string City { get; set; }
    public virtual bool Primary { get; set; }
}

Mapping:

public TblCustomerMap()
{
    Table("tblCustomers");
    LazyLoad();
    Id(x => x.CustomerID).GeneratedBy.Identity().Column("CustomerID");
    Map(x => x.ProfileID).Column("ProfileID");
    Map(x => x.BranchID).Column("BranchID");
    Map(x => x.DateEntered).Column("DateEntered");
    Map(x => x.DateTerminated).Column("DateTerminated");
    Map(x => x.CustTypeID).Column("CustTypeID");
    Map(x => x.CompanyName).Column("CompanyName").Not.Nullable().Length(50);
    Map(x => x.Prefix).Column("Prefix").Not.Nullable().Length(50);
    Map(x => x.FirstName).Column("FirstName").Not.Nullable().Length(50);
    Map(x => x.MiddleName).Column("MiddleName").Not.Nullable().Length(50);
    Map(x => x.LastName).Column("LastName").Not.Nullable().Length(50);
    HasMany(x => x.Address).KeyColumn("CustomerID");
    Map(x => x.PrimaryAddress1).Column("PrimaryAddress1") // from table tbladdress where address is primary and get data from address1 column
    Map(x => x.PrimaryCity).Column("PrimaryCity") // from table tbladdress where address is primary and get data from city column
}

Query:

 var query = session
            .QueryOver<Customer>(() => customer)
            .JoinQueryOver(() => customer.Addresses, () => address)
            .Where(() => address.Primary)
            .List();

        foreach (var customer1 in query)
        {
            customer1.PrimaryAddress1 = customer1.Addresses[0].Address1;
            customer1.PrimaryCity = customer1.Addresses[0].City;
            customer1.PrimaryState = customer1.Addresses[0].StateOrProvince;

        }

New query:

  var query = session.Query<Customer>()
            .SelectMany(c => c.Addresses,
                        (c, a) => new {c, a})
            .Where(cust => cust.a.Primary)
            .Select(item => new CustomerView()
                              {
                                 CustomerID = item.c.CustomerID,
                                 CompanyName=  item.c.CompanyName,
                                 FirstName=  item.c.FirstName,
                                  LastName=item.c.LastName,
                                  Address1=item.a.Address1,
                                  Address2=item.a.Address2,
                                 Address3= item.a.Address3,
                                 City= item.a.City,
                                 StateOrProvince= item.a.StateOrProvince
                              });
        return query.ToList();
  • 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-14T19:08:26+00:00Added an answer on June 14, 2026 at 7:08 pm

    There are (at least) two ways how to achieve this.

    1) The more intuitive and readable is extending DB and adjusting NHibernate mapping. We will need new view viewPrimaryAddress to be created.

    SELECT ***columns*** FROM [tbladdress] WHERE Primary = true
    

    And Customer mapping will look like this:

    Join("[viewPrimaryAddress]", 
    {
      m.Fetch.Join();
      m.KeyColumn("CustomerID");
      m.Map(t => t.PrimaryAddress1).Column("PrimaryAddress1");
      m.Map(t => t.PrimaryCity).Column("PrimaryCity");
    });
    

    And that’s it. One SQL Statement will be issued, so no need to load the Address collection

    2) Oveview of the second approach

    The second approach will create that view by a new class mapping, is a bit complicated, but can be done only on the application side (C# & NHiberante).

    New class PrimaryAddress will be created and contain the ilter on a class level definition (xml mapping example:

    <class name="PrimaryAddress" ... where="Primary = true" >
    

    Then we can extend Customer with new many-to-one relation to PrimaryAddress. So getting properties for column “PrimaryAddress1” and “PrimaryCity” would be done via SQL select filtered in the WHERE Clause.

    EXTENDED:

    Next steps should guide you how to create new mapping, targeting the primary address as the one-to-one property
    1) C# PrimaryAddress:

    public class PrimaryAddress
    {
        public virtual Customer Customer { get; set; }
        public virtual string Address { get; set; }
        public virtual string City { get; set; }
    }
    

    2) Mapping:

    public TblPrimaryAddressMap()
    {
        Table("tbladdress");
        LazyLoad();
        Where("Primary = 1");
        // Id as AddressId
        References(x => x.Customer).Column("CustomerID");
        Map(x => x.Address).Column("PrimaryAddress1")
        Map(x => x.PrimaryCity).Column("PrimaryCity")
    }
    

    3) New property for Customer

    public class Customer
    {  
       ..
       public virtual PrimaryAddress PrimaryAddress { get; set; }
    

    4) New mapping for Customer
    public TblCustomerMap()
    {
    …
    HasOne(x => x.PrimaryAddress)

    At this moment, when you get Customer from session. You can access

    customer.PrimaryAddress.Address
    customer.PrimaryAddress.City
    

    I am working with XML mapping mostly. But from these lines the concept should be clear… Playing with some fetch="join" you can load Customer and its PrimaryAddress in one SQL select

    And if you need even customer property Customer.PrimaryAddress1, just wrap the PrimaryAddress.Address in the getter.

    What you’ll gain more, is possiblity to filter and order by over this new property PrimaryAddress

    NOTE: this approach is fragile for Caching. The reason is, that whil you will change the real Address entity, there is no built mechanism to evict PrimaryAddress. And also, you should force the business layser to allow only Customer with one Address set to primary = true

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

Sidebar

Related Questions

I need to perform a JOIN on a JOIN-ed table, and I'm not sure
I need to write a TSQL procedure which would perform 4 selects and join
I need to perform multiple operations on an Image, for example, I need to
I need to perform a few checks (enable or disable a label elsewhere on
I need to perform a find and replace using XSLT 1.0 which is really
I need to perform an action after a session times out. However I have
I need to perform operations on Lua tables from C where the tables are
I need to perform some operations on all folders within a file share which
I need to perform update/insert simultaneously changing structure of incoming data. Think about Shops
We need to perform the following operation in our database : There is 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.