NHibernate QueryOver 对集合中字段的值求和

发布于 2025-01-07 14:55:23 字数 872 浏览 4 评论 0原文

我有一个包含子集合的父对象:

class Parent {  
    int Id {get;set;}
    ....
    IList<Child> Children {get;set;}
}

 class Child {
    int Id {get;set;}
    int Value {get;set;}
    ...
    Parent Parent {get;set;}
 }

FluentNHibernate 的映射是

ParentMap:

Id(x => x.Id, "id").GeneratedBy.Assigned();
...
HasMany<Child>(x => x.Children).AsBag().KeyColumn("parentid").Inverse()
    .Fetch.Join().Cascade.AllDeleteOrphan();

ChildMap:

Id(x => x.Id).GeneratedBy.Assigned();
....
Map(x => x.Value, "value");
References<Parent>(x => x.Parent, "parentid").NotFound.Ignore();

我必须像这样映射 NHibernate 的 SQL 查询:

select p.id, sum(c.value)
from parent p, child c
where p.id = c.parentid

是否可以通过 QueryOver 转换此查询?

谢谢

I have a parent object containing a collection of children:

class Parent {  
    int Id {get;set;}
    ....
    IList<Child> Children {get;set;}
}

 class Child {
    int Id {get;set;}
    int Value {get;set;}
    ...
    Parent Parent {get;set;}
 }

The mapping whith FluentNHibernate is

ParentMap:

Id(x => x.Id, "id").GeneratedBy.Assigned();
...
HasMany<Child>(x => x.Children).AsBag().KeyColumn("parentid").Inverse()
    .Fetch.Join().Cascade.AllDeleteOrphan();

ChildMap:

Id(x => x.Id).GeneratedBy.Assigned();
....
Map(x => x.Value, "value");
References<Parent>(x => x.Parent, "parentid").NotFound.Ignore();

I have to map whith NHibernate a SQL query like this:

select p.id, sum(c.value)
from parent p, child c
where p.id = c.parentid

Is it possible to translate this query whith QueryOver?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

独孤求败 2025-01-14 14:55:23

你可以试试这个

Child childAlias = null;
var selection = session.QueryOver<Parent>()
    .JoinAlias(p => p.Childs, () => childAlias)
    .Select(Projections.Group(Projections.Id()), Projections.Sum(() => childAlias.Value))
    .List<object[]>()
    .Select(arr => new { Id = (int)arr[0], Sum = (int)arr[1] });

或者这个

Child parentAlias = null;
var selection = session.QueryOver(() => parentAlias)
    .Select(Projections.Id(), Projections.SubQuery(QueryOver.Of<Child>()
        .Where(c => c.Parent == parentAlias)
        .Select(Projections.Sum<Child>(c => c.Value))
    .List<object[]>()
    .Select(arr => new { Id = (int)arr[0], Sum = (int)arr[1] });

或者这个吗

IList selection = from p in session.Query<Parent>
                  select new { p.Id, Sum = p.Childs.Sum(c => c.Value) };

can you try this

Child childAlias = null;
var selection = session.QueryOver<Parent>()
    .JoinAlias(p => p.Childs, () => childAlias)
    .Select(Projections.Group(Projections.Id()), Projections.Sum(() => childAlias.Value))
    .List<object[]>()
    .Select(arr => new { Id = (int)arr[0], Sum = (int)arr[1] });

or this

Child parentAlias = null;
var selection = session.QueryOver(() => parentAlias)
    .Select(Projections.Id(), Projections.SubQuery(QueryOver.Of<Child>()
        .Where(c => c.Parent == parentAlias)
        .Select(Projections.Sum<Child>(c => c.Value))
    .List<object[]>()
    .Select(arr => new { Id = (int)arr[0], Sum = (int)arr[1] });

or this

IList selection = from p in session.Query<Parent>
                  select new { p.Id, Sum = p.Childs.Sum(c => c.Value) };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文