(CS0029) convert a List to ObservableCollection || SQLite

1

I'm trying to create a method that returns a ObservableCollection instead of a List in 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 in the line that tries to return the list return _database.Table<Tarefa>.ToList(); The 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. Thanks in advance.

    
asked by anonymous 13.09.2017 / 19:53

1 answer

2

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

public ObservableCollection<Tarefa> GetTarefas()
{
    return new ObservableCollection<Tarefa>(_database.Table<Tarefa>.ToList());
}
    
13.09.2017 / 19:58