NHibernate 投影重复到列表
我的 nHibernate 投影有问题。我什至不知道我正在尝试的事情是否可以完成,但如果任何 nHibernate 专家可以帮助我,我将不胜感激。
我正在使用 nHibernate 标准来查询数据库,并将结果返回投影到不同的(精简的)对象。
我得到一个返回列表,就像
Id CompanyId Description
1 1 Desc1
1 2 Desc1
1 3 Desc1
2 1 Desc2
2 3 Desc2
3 1 Desc3
3 2 Desc3
3 3 Desc3
我使用这个对象时
int Id
int CompanyId
string Description
获得更像这样的东西
Id CompanyId Description
1 [1, 2, 3] Description
2 [1, 3] Description
3 [1, 2, 3] Description
一样 我正在寻找的是从这样的对象中
int id
List`<int`> companyId
string description
我当前的代码类似于
result = session.CreateCriteria<Object>()
.Add(Restrictions.Eq("SiteId", 616))
.SetProjection(Projections.Distinct(Projections.ProjectionList()
.Add(Projections.Property("Id"), "Id")
.Add(Projections.Property("CompanyId"), "CompanyId")
.Add(Projections.Property("Description"), "Description")
.SetResultTransformer(Transformers.AliasToBean<ObjectReduced>()).List<ObjectReduced>();
所以我想知道这里是否有任何东西我可以做些什么来实现这一点,或者即使这是完全错误的方法并且有更好的方法。
I have a problem with nHibernate projections. I don't even know if what I'm attempting can be done but if any nHibernate experts can help me I'd be grateful.
I'm using nHibernate criteria to query a database and I'm returning the result projected to a different (slimmed down) object.
I get a list of returns like
Id CompanyId Description
1 1 Desc1
1 2 Desc1
1 3 Desc1
2 1 Desc2
2 3 Desc2
3 1 Desc3
3 2 Desc3
3 3 Desc3
when I use this object
int Id
int CompanyId
string Description
What I'm looking for is to get something more like
Id CompanyId Description
1 [1, 2, 3] Description
2 [1, 3] Description
3 [1, 2, 3] Description
From an object like this
int id
List`<int`> companyId
string description
The current code I have is similar to
result = session.CreateCriteria<Object>()
.Add(Restrictions.Eq("SiteId", 616))
.SetProjection(Projections.Distinct(Projections.ProjectionList()
.Add(Projections.Property("Id"), "Id")
.Add(Projections.Property("CompanyId"), "CompanyId")
.Add(Projections.Property("Description"), "Description")
.SetResultTransformer(Transformers.AliasToBean<ObjectReduced>()).List<ObjectReduced>();
So I'd like to know if there is anything here I can do to achieve this, or even if this is completely the wrong approach and there is something better.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能直接在 NHibernate 中执行此操作。实现此目的的最佳方法是获取当前正在执行的结果集,然后使用 CompanyId 填充缩减的对象。类似的:
可能有一种 LINQier 方法可以做到这一点,但这就是我解决类似问题的方法。
You can't do that directly in NHibernate. The best way to achieve this is to get the result set as you are currently doing and then populate the reduced object with the CompanyIds. Something like:
There's probably a LINQier way to do this but this is how I solved a similar problem.