3级表的NHibernate Linq where子句
本文关键字:where 子句 Linq NHibernate 3级 | 更新日期: 2024-10-19 15:06:42
我有三个表,Group->User->Account。组可以有多个用户,用户可以有多个子帐户。
当我尝试用查询获取组中的所有帐户时
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来代替:
public IQueryable<TEntity> FindAll(Expression<Func<TEntity, bool>> expression)
{
var query = _session.Query<TEntity>();
return query.Where(expression);
}
与QueryOver
不同,Linq提供程序支持这类查询(遍历实体层次结构),将其转换为SQL联接。