2

I am joining two tables using primary key and foreign key.

My tables are

  1. Item, Columns are:
    • ItemId int primary key,
    • Name varchar,
    • Price float,
    • CategoryID int foreign key,
    • QtyInStock int
  2. Category, Columns are:
    • Id int pk,
    • Category varchar,
    • Name varchar

I want to select Category on basis of ID=categoryId

I am using NHibernate 4.03 with xml configuration files.

2
  • 1. MVC has nothing to do with that. You can remove that tag. 2. This appears to be a rather basic example. What have you tried so far? 3. Do you want to use QueryOver, Linq, or anything else? Apr 28, 2015 at 12:44
  • Are related your entities by mapping?
    – Najera
    Apr 28, 2015 at 15:08

1 Answer 1

5

For unrelated entities:

var query = from item in session.Query<Item>() 
    join category in session.Query<Category>() on item.CategoryID equals category.Id;

For related entities:

Category catAlias = null;

var query = session.QueryOver<Item>()
    .JoinAlias(x => x.Category, () => catAlias);
1
  • 1
    select c.ItemId,c.Name,c.Price,c.QtyInStock,d.Category from Item as c, Category as d where c.CategoryID=d.Id I want this result in output in mvc 5 nhibernate Apr 30, 2015 at 9:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.