0

我有一个对象,可以说它是从存储库返回的教室,但我对我的视图使用匿名类型,所以我像这样转换它

 return from P in db.ClassRooms
               where P.LocationId == LocationId && P.IsApproved==true
               select new ClassRoomsViewModel
               {
                   Id = P.Id,
                   Created = P.CreatedOn,
                   IsApproved = P.IsApproved,
                   IsDeleted = P.IsDeleted,
                   Desks = ??
               }

问题是我不确定如何处理桌面对象。

在我的 ClassRoomsViewModel 类中,Desks 是一个列表对象

public class ClassRoomsViewModel{

   public long Id { get; set; }
   public DateTime Created { get; set; }
   public List<DeskViewModel> Desks { get; set; }

 }
   public class DeskViewModel{

   public long Id { get; set; }
   public string Name{ get; set; }
 }

教室数据对象作为对课桌对象的引用链接。所以从上面的 linq 查询中,P.Desks.Name 将返回教室中所有对象的名称,用于 linq 查询

4

2 回答 2

4

您需要从数据模型中获取桌子的集合,将每个桌子转换为 a DeskViewModel,并将生成的序列转换为 a List<T>

那看起来像

p.Desks.Select(d => new DeskViewModel { ... }).ToList()
于 2013-06-25T00:21:45.713 回答
0

If P.Desks.Name and P.Desks.Id are arrays you could do it like this with zip.

return from P in db.ClassRooms
           where P.LocationId == LocationId && P.IsApproved==true
           select new ClassRoomsViewModel
           {
               Id = P.Id,
               Created = P.CreatedOn,
               IsApproved = P.IsApproved,
               IsDeleted = P.IsDeleted,
               Desks = P.Desks.Name.Zip(P.Desks.Id,
                           (n, i) => new DeskViewModel { Id = i, Name = n });
           }
于 2013-06-25T00:32:02.897 回答