IAsyncEnumerable cannot be used for parameter of type IEnumerable

1.1k Views Asked by At

I have entities, service and view models. I use the service models in the services and in the service I map from entity to the service model and I return IQueryable<UserServiceModel> and I use the service in the controller but when I try to materialize the result and map it to the view model with Select it throw exception:

ArgumentException: Expression of type 'System.Collections.Generic.IAsyncEnumerable1[TestDriveServiceModel]' cannot be used for parameter of type 'System.Collections.Generic.IEnumerable1[TestDriveServiceModel]' of method 'System.Collections.Generic.List1[TestDriveServiceModel] ToList[TestDriveServiceModel](System.Collections.Generic.IEnumerable1[TestDriveServiceModel])' Parameter name: 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; }
    }
0

There are 0 best solutions below