Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用NHibernate将骨料与实体投影在一起_Nhibernate_Projection_Aggregate Functions - Fatal编程技术网

使用NHibernate将骨料与实体投影在一起

使用NHibernate将骨料与实体投影在一起,nhibernate,projection,aggregate-functions,Nhibernate,Projection,Aggregate Functions,我有一个非常标准的Orders表和一个关联的OrderRows表,比如: Orders [id] INTEGER [name] ... OrderRows [orderId] INTEGER [quantity] INTEGER [unitPrice] SMALLMONEY [description] VARCHAR(...) 在某些情况下,我希望检索订单列表和汇总,大多数情况下我不关心汇总 使用Criteria API,我可以为每个订单创建[quantity]*[unitPrice]的投影

我有一个非常标准的Orders表和一个关联的OrderRows表,比如:

Orders
[id] INTEGER
[name] ...

OrderRows
[orderId] INTEGER
[quantity] INTEGER
[unitPrice] SMALLMONEY
[description] VARCHAR(...)
在某些情况下,我希望检索订单列表和汇总,大多数情况下我不关心汇总


使用Criteria API,我可以为每个订单创建[quantity]*[unitPrice]的投影,但如何在同一结果集中获得订单和投影?

我认为这里的解决方案是创建一个DTO类,然后将其用作结果转换

public class OrderDTO
{
    int Id;
    // Rest of Orders properties you want

    int LineTotal; // [quantity]*[unitPrice]

    public OrderDTO(int id, /* rest of constructor parameters */);
}

session.CreateCriteria(typeof(... /* criteria query here */))
    .SetResultTransformer(new AliasToBeanConstructorResultTransormer(typeof(OrderDTO).GetConstructors()[0]));

您可以计算实体中的总计,并将其存储在数据库中(若不希望每次加载所有行)