使用linq的NHibernate递归查询 [英] NHibernate recursive query using linq

查看:93
本文介绍了使用linq的NHibernate递归查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好.

假设我的桌子上有菜单项.每个项目都有其父项的ID和ID.斯坦达特.如何在不使用字符串sql和后处理(即.ToList().AsHierarchy())的情况下使用linq选择树(列表中或其他列表中的列表)?

Hello.

Let''s say I have table with menu items. Each item have id and id of it''s parent. Standart. How can I select tree (list in list or smth else) using linq without string sql and post-processing (i.e. .ToList().AsHierarchy())? Is that possible?

推荐答案

您应该将子列表映射到菜单项类.这样,菜单项的每个实例还包含其子级,您可以轻松地对其进行迭代.您要做的就是选择ParentId = 0的菜单项.

MenuItem-class:
You should map a list of children to a menu item class. That way each instance of a menu item also contains its children and you can easily iterate them. All you have to do is to select menu items where ParentId=0.

MenuItem-class:
[Bag(0, Name = "Children", Inverse = true, Lazy = CollectionLazy.False)]
[Key(1, Column = "MenuItemParentId")]
[OneToMany(2, ClassType = typeof(MenuItem))]
public virtual IList<menuitem> Children
{
    get { return _children; }
    set { _children = value; }
}
</menuitem>



或通过xml映射(不过,类中仍必须具有Children属性):



Or by xml mapping (must still have the Children property in the class, though):

<bag name="Children" lazy="false" inverse="true">
    <key column="MenuItemParentId" />
    <one-to-many class="Your.Namespace.MenuItemParentId, Your.Namespace" />
</bag>


这篇关于使用linq的NHibernate递归查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆