How to convert List to Ienumerable in async method?

Suriya_KalidossSuriya_Kalidoss Member ✭✭✭

List Collection:
public async Task<List> GetPatientsAsync()
{
return await database.Table().ToListAsync();
}

Tried:
DatabaseManager patient = new DatabaseManager();
var patients = patient.GetPatientsAsync();

        IEnumerable myEnumerable = (IEnumerable)patients;
        IEnumerable<string> myGenericEnumerable = (IEnumerable<string>)patients;

//Need to assign inenumerable to listview but cast exception occurs
PatientsListView.ItemsSource = myGenericEnumerable;

Tagged:

Best Answer

Answers

  • ivanxamarinivanxamarin Member ✭✭

    If you're using System.Collections.Generic.List it should implement IEnumerable and you should just assign it to ItemsSource. I guess the problem is that you don't await for your list and get the Task object.

    DatabaseManager patient = new DatabaseManager();
    var patients = await patient.GetPatientsAsync();
    PatientsListView.ItemsSource = patients;

  • Suriya_KalidossSuriya_Kalidoss Member ✭✭✭

    Hi @ivanxamarin ,
    My protocol is to use async

  • ivanxamarinivanxamarin Member ✭✭
    edited December 2018

    You are indeed using async. You need to await for the result (in your method GetPatientsAsync you are already using await). Try to avoid "var" type so you understand better what happens. You should also use generic List with type argument. Does this explain better?

    `DatabaseManager patient = new DatabaseManager();

        Task<List<Patient>> taskPatients = patient.GetPatientsAsync();
    
        // Here you can do any work for which you don't need patients
    
        List<Patient> patients = await taskPatients
        PatientsListView.ItemsSource = patients;`
    
  • Suriya_KalidossSuriya_Kalidoss Member ✭✭✭

    Hi @ivanxamarin
    ** My database code in DataBaseManager class:**
    public async Task<List> GetPatientsAsync()
    {
    return await database.Table().ToListAsync();
    }

    I need to bind it to my listview
    DatabaseManager patient = new DatabaseManager();
    var patients = patient.GetPatientsAsync();

    }

    listviewpage.cs
    DatabaseManager patient = new DatabaseManager();
    var patients = patient.GetPatientsAsync();

    MyListView.ItemsSource = patients;
    //Here I got exception unable to bind the list to enumerable how to cast this I'm a beginner in the database. I need to use aync only

  • JohnHairJohnHair GBMember ✭✭✭✭✭

    @Suriya_Kalidoss said:
    Hi @ivanxamarin
    ** My database code in DataBaseManager class:**
    public async Task<List> GetPatientsAsync()
    {
    return await database.Table().ToListAsync();
    }

    I need to bind it to my listview
    DatabaseManager patient = new DatabaseManager();
    var patients = patient.GetPatientsAsync();

    }

    listviewpage.cs
    DatabaseManager patient = new DatabaseManager();
    var patients = patient.GetPatientsAsync();

    MyListView.ItemsSource = patients;
    //Here I got exception unable to bind the list to enumerable how to cast this I'm a beginner in the database. I need to use aync only

    Your patients variable is a task, it is not a list. You need to await it to get the results. That is how async works.

  • Suriya_KalidossSuriya_Kalidoss Member ✭✭✭

    Hi @JohnHair ,
    I had understood what you said, But how to make this workable..

  • ivanxamarinivanxamarin Member ✭✭

    just replace this:

    var patients = patient.GetPatientsAsync();

    with this:

    var patients = await patient.GetPatientsAsync();

    and anyway you should read about asynchronous methods in .net

  • Suriya_KalidossSuriya_Kalidoss Member ✭✭✭

    Hi @ivanxamarin ,
    I'm getting this exception
    the 'await' operator can only be used within an async method. consider marking this method with async modifier and change its return type to task

  • ivanxamarinivanxamarin Member ✭✭

    So, in which method are you using this code? It has to be async also.

  • Suriya_KalidossSuriya_Kalidoss Member ✭✭✭

    already this is in aync
    public async Task GetPatientsAsync()
    {
    return await database.Table().ToListAsync();
    }
    Sorry if had tell anything wrong

  • ivanxamarinivanxamarin Member ✭✭

    Where is this code?

    DatabaseManager patient = new DatabaseManager();
    var patients = patient.GetPatientsAsync();

        IEnumerable myEnumerable = (IEnumerable)patients;
        IEnumerable<string> myGenericEnumerable = (IEnumerable<string>)patients;
    

    //Need to assign inenumerable to listview but cast exception occurs
    PatientsListView.ItemsSource = myGenericEnumerable;

    In which method? That method has to be async. Can you paste whole .cs file with this code?

  • ivanxamarinivanxamarin Member ✭✭
    edited December 2018

    You can try this:

    DatabaseManager patient = new DatabaseManager(); List<Patient> patients; Task.Run(async () => { patients = await patient.GetPatientsAsync(); }).Wait(); PatientsListView.ItemsSource = patients;

    but I woudln't recommend it untill you completely understand asynchronous methods.

  • Suriya_KalidossSuriya_Kalidoss Member ✭✭✭

    Hi @ivanxamarin
    Listview Page cs code:
    public PatientListPage()
    {
    InitializeComponent();
    DatabaseManager patient = new DatabaseManager();

           var patients = await patient.GetPatientsAsync();
            PatientsListView.ItemsSource = patients;
    
        }
    

    DataManager class: I had used SQLite

    public async Task<List<Patient>> GetPatientsAsync()
        {
            return await database.Table<Patient>().ToListAsync();
        }
    

    this is my try:
    IEnumerable myEnumerable = (IEnumerable)patients;
    IEnumerable myGenericEnumerable = (IEnumerable)patients;

  • ivanxamarinivanxamarin Member ✭✭

    Ok, check the comment above. Also, you are using Xamarin Forms without MVVM, that is also not good practice.

  • JohnHairJohnHair GBMember ✭✭✭✭✭

    @Suriya_Kalidoss said:
    Hi @ivanxamarin
    Listview Page cs code:
    public PatientListPage()
    {
    InitializeComponent();
    DatabaseManager patient = new DatabaseManager();

           var patients = await patient.GetPatientsAsync();
            PatientsListView.ItemsSource = patients;
    
        }
    

    You should not be trying to use async methods in a constructor.

  • ivanxamarinivanxamarin Member ✭✭

    Definitely. If it's working, you should move the code to:

    protected override async void OnAppearing()

  • Suriya_KalidossSuriya_Kalidoss Member ✭✭✭

    Hi @ivanxamarin ,
    The above code does not works

  • JohnHairJohnHair GBMember ✭✭✭✭✭

    @Suriya_Kalidoss said:
    Hi @ivanxamarin ,
    The above code does not works

    Saying 'does not works' is not helpful. What doesn't work? Paste your code.

  • ivanxamarinivanxamarin Member ✭✭

    Ok, add this to your ListViewPage:

    `protected override async void OnAppearing()

    {
    DatabaseManager patient = new DatabaseManager();
    var patients = await patient.GetPatientsAsync();
    PatientsListView.ItemsSource = patients;
    }`

    and remove same code from constructor

Sign In or Register to comment.