Home > Net >  <System.Collections.Generic.List<EduCube.Models.StaffModel>> to System.Collections.Gener
<System.Collections.Generic.List<EduCube.Models.StaffModel>> to System.Collections.Gener

Time:08-24

I am busy with a project where I want to get a list of lecturers to display inside a picker for the admin to assign a lecturer to a subject.

I get all off the staff in a list :

public interface IStaffService
    {
        //Initialize CRUD operations
        Task<int> AddStaff(StaffModel staffModel);
        Task<List<StaffModel>> GetStaffList();
        Task<int> EditStaff(StaffModel staffModel);
        Task<int> DeleteStaff(StaffModel staffModel);
        Task<bool> AdminLoginAuth(string email, string password);
    }

Then call the repository to populate the list (GetStaffList):

 private SQLiteAsyncConnection _dbConnection;

        //setup using path
        private async Task SetUpDb()
        {
            if (_dbConnection == null)
            {
                string dbPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Staff.db3");
                _dbConnection = new SQLiteAsyncConnection(dbPath);
                await _dbConnection.CreateTableAsync<StaffModel>();
            }
        }

        public async Task<List<StaffModel>> GetStaffList()
        {
            await SetUpDb();
            var staffList = await _dbConnection.Table<StaffModel>().ToListAsync();
            return staffList;
        }

I then make Repos to call them (App.xaml.cs):

public partial class App : Application
{

    public static IStaffService StaffRepo { get; private set; }
    public static IStudentService StudentRepo { get; private set; }
    public static ISubjectService SubjectRepo { get; private set; }



    public App(IStaffService staffRepo, IStudentService studentRepo, ISubjectService subjectRepo)
    {
        InitializeComponent();


        MainPage = new AppShell();

        //Initializing our repos
        StaffRepo = staffRepo;
        StudentRepo = studentRepo;
        SubjectRepo = subjectRepo;



    }
}

and finally here where I call for a list of lecturers from the subjectsViewmodel:

public partial class AddUpdateSubjectViewModel : ObservableObject
    {

        

        [ObservableProperty]
        public SubjectModel _subjectDetail = new SubjectModel();

            private readonly ISubjectService _subjectRepository;
            public AddUpdateSubjectViewModel(ISubjectService subjectService)
            {
            _subjectRepository = subjectService;

            listOfLecturers =  new List<StaffModel>(App.StaffRepo.GetStaffList());
            


            }






        [ObservableProperty]
        List<StaffModel> listOfLecturers;

 [ICommand]
            public async void AddUpdateSubject()
            {
            int response = -1;
            if (SubjectDetail.SubjectID > 0)
            {
                response = await _subjectRepository.EditSubject(SubjectDetail);
            }
            else
            {
                response = await _subjectRepository.AddSubject(new Models.SubjectModel
                {
                    SubjectTitle = SubjectDetail.SubjectTitle,
                    SubjectCode = SubjectDetail.SubjectCode,
                    SubjectLecturer = SubjectDetail.SubjectLecturer,
                    SubjectDescription = SubjectDetail.SubjectDescription,
                    SubjectCredits = SubjectDetail.SubjectCredits,
                    SubjectPrice = SubjectDetail.SubjectPrice,
                    SubjectDate = SubjectDetail.SubjectDate,
                    SubjectTime = SubjectDetail.SubjectTime,
                    SubjectImage = SubjectDetail.SubjectImage,
                    SubjectHours = SubjectDetail.SubjectHours,
                    SubjectVenue = SubjectDetail.SubjectVenue
                });
            }

                if (response > 0)
                {
                    await Shell.Current.DisplayAlert("Subject info saved", "Record Saved", "OK");
                   
                }
                else
                {
                    await Shell.Current.DisplayAlert("Not added", "Something went wrong while adding record", "OK");
                }

            }
            

        }
    }


The issue I get is that it says that It wants an IEnumerable instead of a list. Any fixes for this or workarounds would be appreciated.

If I click potential fixes it gives out:

listOfLecturers =  new List<StaffModel>((IEnumerable<StaffModel>)App.StaffRepo.GetStaffList());

which removes the error but breaks the code when I navigate to that page with error:

"System.InvalidCastException: 'Unable to cast object of type 'AsyncStateMachineBox1[System.Collections.Generic.List1[EduCube.Models.StaffModel],EduCube.Services.StaffRepository d__3]' to type 'System.Collections.Generic.IEnumerable`1[EduCube.Models.StaffModel]'."

Thank You

CodePudding user response:

List is IEnumerable.

The problem is that you are passing in a Task<> instead of a List<> because App.StaffRepo.GetStaffList() is an async method. If you can't change AddUpdateSubjectViewModel to async (and use await) then change your repo method to the following:

public List<StaffModel> GetStaffList()
        {
            await SetUpDb();
            var staffList = _dbConnection.Table<StaffModel>().ToList();
            return staffList;
        }

CodePudding user response:

You can try to use the Task<TResult>.Result to get the TResult, such as:

listOfLecturers = App.StaffRepo.GetStaffList().Result;
  • Related