Fluent NHibernate Order by Property in related Entity using QueryOver
I have items and itemgroup tables in my database:
CREATE TABLE [items](
[item_id] [int] IDENTITY(1,1) NOT NULL,
[item_name] [varchar](50) NOT NULL,
[group_id] [int] NOT NULL
)
CREATE TABLE [itemgroup](
[group_id] [int] IDENTITY(1,1) NOT NULL,
[group_name] [varchar](50) NULL
)
and here are mapping classes for these entities:
public class ItemMap : ClassMap<Item>
{
public ItemMap()
{
Table("items");
Id(x => x.Id).Column("item_id");
Map(x => x.Name).Column("item_name");
References(x => x.ItemGroup).Column("group_id").Fetch.Join();
}
}
public class ItemGroupMap : ClassMap<ItemGroup>
{
public ItemGroupMap()
{
Table("itemgroup");
Id(x => x.Id).Column("group_id");
Map(x => x.Name).Column("group_name");
}
}
How can I get all items from the database ordered by group name using QueryOver?
I know how to accomplish it using Criteria (using alias).
var criteria = Session.CreateCriteria<Item>()
.CreateAlias("ItemGroup", "group")
.AddOrder(Order.Asc("group.Name"));
I tried to create ItemGroup alias and use it with QueryOver, but my result was sorted by item name not b开发者_高级运维y itemgroup name.
It would look something like this:
Item itemAlias = null;
ItemGroup itemGroupAlias = null;
session.QueryOver<Item>(() => itemAlias)
.JoinAlias(() => itemAlias.ItemGroup, () => itemGroupAlias)
.OrderBy(() => itemGroupAlias.Name).Asc;
精彩评论