假设我有一个带有一些字段的实体和一个像这样的集合
Entity
int Id {get; set;}
string Name {get; set;}
string Title {get; set;}
List<Order>Orders {get; set;}
Order
int Id {get; set;}
string Name {get; set;}
Entity Entity {get; set;}
现在我想创建EntityOrderViewModel,它只代表两个权限中的少数属性。喜欢这个
EntityOrderViewModel
int Id {get; set;}
string EntityName {get; set;}
string EntityTitle {get; set;}
string OrderName {get; set;}
int OrderId {get; set;}
EntityOrderViewModel(Entity x)
{
Id = 1;
EntityName = x.Name;
EntityTitle = x.Title;
foreach(Order order in x.Orders)
{
OrderName = order.Name;
}
}
public static List<EntityOrderViewModel> FromMyDomain(IList<Entity> x)
{
List<EntityOrderViewModel> vm= new List<EntityOrderViewModel>();
foreach (Entity e in x)
{
vm.Add(new EntityOrderViewModel(e));
}
return vm;
}
现在从我的控制器我想调用所有这样的实体
//opensession
// open transaction
List<Entity> data = session.Query<Entity>().ToList();
//end transaction
transaction.commit();
//end session
return EntityOrderViewModel.FromMyDomain(data);
答案 0 :(得分:0)
因为你在控制器中有会话,为什么不直接给视图模型加水?
from entity in session.Query<Entity>()
join order in session.Query<Order> join entity.EntityId on order.Entity.EntityId
select new EntityOrderDTO // or anon
{
EntityId = entity.EntityId,
Name = entity.Name,
OrderName = order.OrderName
}
加入将照顾你的n + 1