nHibernate中的Dynamic QueryOver [英] Dynamic QueryOver in nHibernate

查看:189
本文介绍了nHibernate中的Dynamic QueryOver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类似于动态LINQ OrderBy ,我想制作一个动态的QueryOver-OrderBy。然而,当我这样做:

Similar to 'Dynamic LINQ OrderBy' I would like to make a dynamic QueryOver-OrderBy. However, when I do this:

query.OrderBy(h => h.GetType().GetProperty(sort.Member).GetValue(h, null)).Asc

我得到一个异常说:

Unrecognised method call in epression h.GetType().GetProperty(value(Domain.Model.Repository+<>c__DisplayClass15).sort.Member).GetValue(h, null)

显然,nHibernate有一些问题了解发生了什么。有没有人想知道如何解决这个特定的问题?

Apparently, nHibernate has some trouble understanding what's going on. Does anyone have an idea on how to solve this particular issue?

推荐答案

解决方案在链接的问题不起作用当连接多个实体时,仍然是一个问题,并且应该根据来自不同实体的属性的组合进行排序。我可以想到的唯一实际的解决方案是从 sort.Member 创建一个字典到 Projection.Property

The solution in the linked question doesn't work as there still is a problem when multiple entities are joined and sorting should be done on a combination of properties from different entities. The only practical solution I could think was to create a dictionary from sort.Member to a Projection.Property.

var dict = new Dictionary<string, string>();
dict.Add("sortMember", "entityAlias.Property");
var sortOn = dict[sort.Member];
query.OrderBy(Projections.Property(sortOn)).Asc;

这个效果很好。该字典基本上替代了整个if-else结构,一个简单的循环添加了orderBy。我想我可以以类似的方式修改过滤部分,但是还没有时间去工作。

This works quite well. The dictionary basically replaces the whole if-else construction and a simple loop adds the orderBy. I guess I can modify the filtering-part in a similar fashion, but haven't had time to work on that part yet.

这篇关于nHibernate中的Dynamic QueryOver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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