我想构建一个“管理用户”视图,并且当我使用选择查询时遇到错误
不能在LINQ to Entities查询中构造实体或复杂类型'Memberships.Models.UserViewModel'。
public static async Task GetUsers(this List<UserViewModel> users)
{
var db = ApplicationDbContext.Create();
users.AddRange(await (from u in db.Users
select new UserViewModel
{
Id = u.Id,
Email = u.Email,
FirstName = u.FirstName
}).OrderBy(o => o.Email).ToListAsync());
}
模型
public class UserViewModel
{
[Display(Name = "User Id")]
public string Id { get; set; }
[Required]
[EmailAddress]
[Display(Name = "Email")]
public string Email { get; set; }
[Display(Name ="First Name")]
[StringLength(30, ErrorMessage ="The {0} mus be at least {1} characters long", MinimumLength =2)]
public string FirstName { get; set; }
[Required]
[StringLength(100)]
[DataType(DataType.Password)]
[Display(Name ="Password")]
public string Password { get; set; }
}
重新设计这部分代码以使其正常工作的最佳和最短方法是什么?我不想要复杂的东西,我只需要重新设计此代码即可使用 有帮助的人吗?