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? Commented Apr 28, 2015 at 12:44
  • Are related your entities by mapping? Commented Apr 28, 2015 at 15:08

1 Answer 1

6

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);
Sign up to request clarification or add additional context in comments.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.