繁体   English   中英

NHibernate Linq where子句,用于3个级别的表

[英]NHibernate Linq where clause for 3 level tables

我有三个表,组->用户->帐户。 组可以有多个用户,用户可以有多个帐户。

当我尝试使用查询获取组中的所有帐户时

var accounts = accountRepository.FindAll(x => x.User.Group.Id == groupId);

其中FindAll是公共存储库中的方法

/// <summary>
/// Find all entities with filter
/// </summary>
/// <param name="expression">Linq expression</param>
/// <returns>IQueryOver of entity of type TEntity</returns>
public IQueryOver<TEntity> FindAll(Expression<Func<TEntity, bool>> expression)
{
    var query = _session.QueryOver<TEntity>();
    query.Cacheable().CacheMode(CacheMode.Normal);
    return query.Where(expression);
}

我的查询无法找到网上论坛中的所有帐户,因为它引发了错误消息

could not resolve property: User.Group.Id

有什么更好的方法来编写此查询

使用Linq代替NHibernate:

public IQueryable<TEntity> FindAll(Expression<Func<TEntity, bool>> expression)
{
    var query = _session.Query<TEntity>();
    return query.Where(expression);
}

QueryOver不同,Linq提供程序支持此类查询(遍历实体层次结构),将其转换为SQL连接。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM