如何转换 RoleViewModel 列表中的匿名列表?
How to convert anonymous list in list of RoleViewModel?
在我的操作方法中,我 运行 以下查询 returns 一个列表:
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();
我有一个格式相同的 viemodel,如下所示:
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; }
}
将查询结果转换为视图模型列表的最佳方法是什么?
如果您不想让操作方法识别视图模型类型,您可以尝试以下操作。
- 在初始列表上使用
Select
并为每个项目构建 RoleViewModel
。
使用 AutoMapper 库来完成这项工作。
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));
var list = db.WorkRoles.
Join(db.WorkRolesUsersDetails,
o => o.WorkRoleId, od => od.WorkRoleId,
(o, od) => new RoleViewModel(
o.WorkRoleId,
o.RoleName,
o.RoleDescription,
o.CompanyId,
od.WRUDId,
od.UserDetailsId,
od.FocusStart,
od.FocusEnd
)).ToList();
在我的操作方法中,我 运行 以下查询 returns 一个列表:
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();
我有一个格式相同的 viemodel,如下所示:
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; }
}
将查询结果转换为视图模型列表的最佳方法是什么?
如果您不想让操作方法识别视图模型类型,您可以尝试以下操作。
- 在初始列表上使用
Select
并为每个项目构建RoleViewModel
。 使用 AutoMapper 库来完成这项工作。
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));
var list = db.WorkRoles.
Join(db.WorkRolesUsersDetails,
o => o.WorkRoleId, od => od.WorkRoleId,
(o, od) => new RoleViewModel(
o.WorkRoleId,
o.RoleName,
o.RoleDescription,
o.CompanyId,
od.WRUDId,
od.UserDetailsId,
od.FocusStart,
od.FocusEnd
)).ToList();