2012-02-28 74 views
1

我有一个类plannedSalesInYear这个类的NHibernate和集团通过另一个类

public class PlannedSalesInYear{ 
     Vehicle _vehicle; 
     int _year; 
     int _plannedSales; 

     ... Fields as Properties ... 

} 

映射,所以我离开它应该是没有火箭科学。 现在我想选择导致字典的所有车辆的数据。

的解释应该是这样的:

IDictionary<Vehicle, IDictionary<int, int>> 

这可能使用休眠(首选标准API)? 我想避免Linq因为性能问题。所以道应直接选择到词典。

预先感谢托比

回答

0

2往返

// get the results 
IDictionary<Vehicle, IDictionary<int, int>> results = session.Query<PlannedSalesInYear>() 
    .Select(ps => new { VehicleId = ps.Vehicle.Id, ps.Year, ps.PlannedSales }) 
    .AsEnumerable() 
    .GroupBy(a => a.VehicleId) 
    .ToDictionary(
     gr => session.Load<Vehicle>(gr.Key), 
     gr => (IDictionary<int, int>)gr.ToDictionary(a => a.Year, a => a.PlannedSales)); 

// initialize the vehicles 
session.QueryOver<Vehicle>().WhereRestrictionOn(v => v.Id).IsIn(results.Keys.Select(v => v.Id).ToArray()).List(); 

或一个往返与多个数据

// initialize the vehicles 
session.Query<PlannedSalesInYear>() 
    .Fetch(ps => ps.Vehicle) 
    .AsEnumerable() 
    .GroupBy(a => a.Vehicle) 
    .ToDictionary(
     gr => gr.Key, 
     gr => (IDictionary<int, int>)gr.ToDictionary(a => a.Year, a => a.PlannedSales));