How to join two tables in Nhibernate

List<object[]> olist = null;

olist = (_session.CreateQuery("Select pc.Id as Id,pct.DescEn as DescEn,pct.DescAr as DescAr,pc.ContentEn as ContentEn,pc.ContentAr as ContentAr from ProjectCharter pc,ProjectCharterTemplate pct where pct.Id=pc.PRC_PCT_ID and pc.PRC_PRJ_ID=1").List<object[]>()).ToList<object[]>();

This is my query, I want to join the two tables and get the output when I run this db, I get the perfect answer, but when I run it through C # with nhibernate mapping. I get errors.

Can I request this method or is there any other method for joining two tables.

Thanks in advance.

+4
source share
1 answer

It is easy. Surprisingly easy. Check out

So, the above query in QueryOver might look like this:

// alias for later use
ProjectCharter project = null;
ProjectCharterTemplate template = null;

var list = session
    .QueryOver<ProjectCharter>(() => project)
    // the JOIN will replace the WHERE in the CROSS JOIN above
    // it will be injected by NHibernate based on the mapping
    // relation project has many-to-one template
    .JoinQueryOver<ProjectCharterTemplate>(c => c.Templates, () => template)
    .Select(
        // select some project properties
        _ => project.ContentEnglish,
        ...
        // select some template properties
        _ => template.DescriptionEnglish,
     )
    .List<object[]>();
+3
source

All Articles