Reputation: 1332
I have an object lets say its classrooms that is returned from the repository but I use an anonymous type for my View so I convert it like so
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 = ??
}
the problem is I am not sure how to handle the desk object.
In my ClassRoomsViewModel class Desks is a list object
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; }
}
The classrooms dataobject is link as a reference to the Desk Object. so from the above linq query P.Desks.Name will return the name of all objects in the classroom for the linq query
Upvotes: 0
Views: 4499
Reputation: 70513
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 });
}
Upvotes: 0
Reputation: 887225
You need to take the collection of desks from the data model, convert each one to a DeskViewModel
, and convert the resulting sequence to a List<T>
.
That would look something like
p.Desks.Select(d => new DeskViewModel { ... }).ToList()
Upvotes: 4