如何将Querytable转换为ObservableCollection

本文关键字:ObservableCollection 转换 Querytable | 更新日期: 2023-09-27 18:29:50

SQlite 新手

我没能在C#(WindowsStore)中将QueryTable#SQlite转换为ObservableCollection。

我的意思是,我创建了一个名为person的类,它继承自BindableBase。(型号):

class Person : BindableBase
{
    private int _id;
    public int id { get { return _id; } set {SetProperty(ref _id, value);} }
    private string _Name;
    public string Name { get { return _Name; } set {SetProperty(ref _Name, value);} }
    private string _LastName;
    public string LastName { get {return _LastName;} set {SetProperty(ref _LastName, value);} }
    private double _Celphone;
    public double Celphone { get {return _Celphone;} set {SetProperty(ref _Celphone, value);} }
}

我创建了另一个名为PersonCollection(模型)的类

class PersonCollection: ObservableCollection<Person>
{
}

好的,现在,当我尝试用表(ViewModel)的数据填充集合时,我不能将tablequery转换为ObservableCollection。如何解决此问题。

我的ViewModel类:

class PersonVM: BindableBase
{       
    private PersonCollection _PersonList;
    public PersonCollection PersonList {get {return _PersonList;} 
                                        set {SetProperty(ref _PersonList, value);} }

    public async Task<bool> GetPersons()
    {
        try
        {
            var dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "db.db3");
            using (var db = new SQLite.SQLiteConnection(dbPath))
            {
                var listadepersonas = from x in db.Table<Person>() select x;
                foreach (var persona in listadepersonas)
                {
                    PersonList.Add(new Person() 
                    { id = persona.id, Name = persona.LastName, 
                      LastName = persona.LastName, Celphone = persona.Celphone });
                }
                db.Dispose();
                db.Close();
            }
            return true;
        }
        catch (Exception ex)
        {
            string sErr = ex.Message;
            return false;
        }                           
}

异常返回给我:例如Message="对象引用未设置为对象实例。"

如何将Querytable转换为ObservableCollection

您没有初始化PersonList,因此PersonList为空。这就是为什么你会得到NullReferenceExceptionObject reference not set to an instance of an object.

public async Task<bool> GetPersons()
{
    try
    {
        var dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "db.db3");
        //Initialize the PersonList, so PersonList won't be null.
        PersonList = new PersonCollection();
        using (var db = new SQLite.SQLiteConnection(dbPath))
        {
            var listadepersonas = from x in db.Table<Person>() select x;
            foreach (var persona in listadepersonas)
            {
                PersonList.Add(new Person()
                {
                    id = persona.id,
                    Name = persona.LastName,
                    LastName = persona.LastName,
                    Celphone = persona.Celphone
                });
            }
            db.Dispose();
            db.Close();
        }
        return true;
    }
    catch (Exception ex)
    {
        string sErr = ex.Message;
        return false;
    }
}