假设我有一个带有一些字段的实体和一个像这样的集合
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);