Reputation: 1189
In my action method I am running the following query which returns a list :
var list = db.WorkRoles.
Join(db.WorkRolesUsersDetails,
o => o.WorkRoleId, od => od.WorkRoleId,
(o, od) => new
{
WorkRoleId = o.WorkRoleId,
RoleName = o.RoleName,
RoleDescription = o.RoleDescription,
CompanyId = o.CompanyId,
WRUDId = od.WRUDId,
UserDetailsId = od.UserDetailsId,
FocusStart = od.FocusStart,
FocusEnd = od.FocusEnd
}).ToList();
I have a viemodel in the same format looking like this :
public class RoleViewModel
{
public RoleViewModel(int workRoleId, string roleName, string roleDescription, int companyId, int wRUDId, string userDetailsId, DateTime focusStart, DateTime focusEnd)
{
WorkRoleId = workRoleId;
RoleName = roleName;
RoleDescription = roleDescription;
CompanyId = companyId;
WRUDId = wRUDId;
UserDetailsId = userDetailsId;
FocusStart = focusStart;
FocusEnd = focusEnd;
}
int WorkRoleId { get; set; }
string RoleName { get; set; }
string RoleDescription { get; set; }
int CompanyId { get; set; }
int WRUDId { get; set; }
string UserDetailsId { get; set; }
DateTime FocusStart { get; set; }
DateTime FocusEnd { get; set; }
}
What is the best way to convert the result of my query to a list of my viewmodel?
One thing I've tried from answers here on stackoverflow is :
var list = db.WorkRoles.
Join(db.WorkRolesUsersDetails,
o => o.WorkRoleId, od => od.WorkRoleId,
(o, od) => new
{
WorkRoleId = o.WorkRoleId,
RoleName = o.RoleName,
RoleDescription = o.RoleDescription,
CompanyId = o.CompanyId,
WRUDId = od.WRUDId,
UserDetailsId = od.UserDetailsId,
FocusStart = od.FocusStart,
FocusEnd = od.FocusEnd
}).ToList()
.Select(item => new RoleViewModel(
item.WorkRoleId,
item.RoleName,
item.RoleDescription,
item.CompanyId,
item.WRUDId,
item.UserDetailsId,
item.FocusStart,
item.FocusEnd));
But, sadly, what this got mew wasn't a list og RoleViewModel
s. Here's a copy from the value part in the debugger :
{System.Linq.Enumerable.WhereSelectListIterator<<>f__AnonymousType6<int, string, string, int, int, string, System.DateTime, System.DateTime>, eksp.Models.RoleViewModel>}
Upvotes: 1
Views: 839
Reputation: 14007
You have to use ToList
to get the list of your items. You are assigning the query to your list
variable. The query is deferred, that means it will not be executed until you actually retrieve a result with a call to ToList
, ToArray
, First
etc.:
List<RoleViewModel> list = db.WorkRoles.
Join(db.WorkRolesUsersDetails,
o => o.WorkRoleId, od => od.WorkRoleId,
(o, od) => new
{
WorkRoleId = o.WorkRoleId,
RoleName = o.RoleName,
RoleDescription = o.RoleDescription,
CompanyId = o.CompanyId,
WRUDId = od.WRUDId,
UserDetailsId = od.UserDetailsId,
FocusStart = od.FocusStart,
FocusEnd = od.FocusEnd
}).ToEnumerable()
.Select(item => new RoleViewModel(
item.WorkRoleId,
item.RoleName,
item.RoleDescription,
item.CompanyId,
item.WRUDId,
item.UserDetailsId,
item.FocusStart,
item.FocusEnd)).ToList();
The best tip I can give you: Avoid using var
and specify the types explicitly where you can. If you would have done that, your compiler would have given you an error.
Upvotes: 3