nHibernate中的Dynamic QueryOver

编程入门 行业动态 更新时间:2024-10-11 17:19:32
本文介绍了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

本文发布于:2023-11-28 17:40:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1643345.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:nHibernate   Dynamic   QueryOver

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!