我有实体,服务和视图模型。我在服务中使用服务模型,并在从实体到服务模型的服务中使用服务模型,并返回IQueryable<UserServiceModel>
,并在控制器中使用服务,但是当我尝试实现结果并将其映射到视图时带有Select it throw异常的模型:
ArgumentException:类型为'System.Collections.Generic.List
1[TestDriveServiceModel]' cannot be used for parameter of type 'System.Collections.Generic.IEnumerable
1 [TestDriveServiceModel])方法'System.Collections.Generic.IAsyncEnumerable1[TestDriveServiceModel] ToList[TestDriveServiceModel](System.Collections.Generic.IEnumerable
1 [TestDriveServiceModel]'的表达式 参数名称:arg0
// the service map from entities to service models and returns them
// userServiceModels is the result of the service
// Here the error is thrown
var viewModel = await userServiceModels.Select(usm => new UserViewModel()
{
TestDrivesCount = usm.TestDrives.Count()
}).ToListAsync();
public class UserServiceModel : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
public ICollection<TestDriveServiceModel> TestDrives { get; set; } = new List<TestDriveServiceModel>();
}
public class TestDriveServiceModel
{
public string Id { get; set; }
public string CarId { get; set; }
public CarServiceModel Car { get; set; }
public string UserId { get; set; }
public UserServiceModel User { get; set; }
public string StatusId { get; set; }
public StatusServiceModel Status { get; set; }
public DateTime ScheduleDate { get; set; }
public string Comment { get; set; }
}
public class User : IdentityUserEntity
{
public string FirstName { get; set; }
public string LastName { get; set; }
public ICollection<TestDriveEntity> TestDrives { get; set; } = new List<TestDriveEntity>();
}
public class TestDriveEntity
{
public string Id { get; set; }
[Required]
public string CarId { get; set; }
public BaseCarEntity Car { get; set; }
[Required]
public string UserId { get; set; }
public User UserEntity { get; set; }
[Required]
public string StatusId { get; set; }
public Status StatusEntity { get; set; }
[Required]
public DateTime ScheduleDate { get; set; }
public string Comment { get; set; }
}