NHibernate 投影重复到列表

发布于 2024-09-24 15:36:14 字数 1355 浏览 2 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

把人绕傻吧 2024-10-01 15:36:14

您不能直接在 NHibernate 中执行此操作。实现此目的的最佳方法是获取当前正在执行的结果集,然后使用 CompanyId 填充缩减的对象。类似的:

var temp = 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>();
var groups = temp.GroupBy(x => x.Id);
var result = new List<ObjectReduced>(groups.Count());
foreach (var member in groups)
{
    var first = member.First();
    var companyIds = member.Select(x => x.CompanyId);
    foreach (var companyId in companyIds)
    {
        first.CompanyIds.Add(companyId);
    }
    result.Add(first);
}
return result;

可能有一种 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:

var temp = 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>();
var groups = temp.GroupBy(x => x.Id);
var result = new List<ObjectReduced>(groups.Count());
foreach (var member in groups)
{
    var first = member.First();
    var companyIds = member.Select(x => x.CompanyId);
    foreach (var companyId in companyIds)
    {
        first.CompanyIds.Add(companyId);
    }
    result.Add(first);
}
return result;

There's probably a LINQier way to do this but this is how I solved a similar problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文