(CS0029) convert a List into Observablecollection || Sqlite

Asked

Viewed 186 times

1

I’m trying to create a method that returns a ObservableCollection in place of a Listin the Sqlite

namespace Projeto_03.DataBase
{
    public class TarefasDataAccess
    {
        private SQLiteConnection _database;

        public TarefasDataAccess()
        {
            _database = DependencyService.Get<IDatabase>().GetConnection();
            _database.CreateTable<Tarefa>();
        }

        public ObservableCollection<Tarefa> GetTarefas()//era List<Tarefa>
        {
            return _database.Table<Tarefa>.ToList();
        }
    }
}

only on the line that tries to return the list return _database.Table<Tarefa>.ToList(); Error occurs:

Error CS0029: Cannot implicitly convert type 'System.Collections.Generic.List<Projeto_03.Model.Tarefa>' to 'System.Collections.ObjectModel.ObservableCollection<Projeto_03.Model.Tarefa>

namespace Projeto_03.ViewModel
{
    public partial class TelaPrincipalViewModel : ContentPage
    {
        public ObservableCollection<Tarefa> Tarefas { get; set; }//era List<Tarefa>

        public TelaPrincipalViewModel()
        {
            Tarefas = new TarefasDataAccess().GetTarefas();
        }
    }
}

if anyone can help. I thank you in advance.

1 answer

2


Just create a new instance of ObservableCollection passing the list as parameter.

public ObservableCollection<Tarefa> GetTarefas()
{
    return new ObservableCollection<Tarefa>(_database.Table<Tarefa>.ToList());
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.