类似于动态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
发布评论