简体   繁体   中英

Querying sqlite table with linq returns a list with empty items

I have table in a sqlite database in a Xamarin Forms app with about 12000 items stored in it. When I try to return only one column with the following query, I got a list with all 12000 values but the entries of the list are null.

Code for the class:

public class BaseModel
{
        private int? _PrimaryKey;
        private string _Code;
        private string _Name;

        [PrimaryKey, Required, NotNull]
        public int? PrimaryKey { get { return _PrimaryKey; } set { _PrimaryKey = value; } }

        public string Code { get { return _Code; } set { _Code = value; } }

        public string Name { get { return _Name; } set { _Name = value; } }
}

I to query the table with the following line:

internal T GetInsertItem<T>() where T : BaseModel, new()
{
    T item = new T();
    //...
    List<int?> items = new List<int?>(_Conn.Table<T>().Select(ac => ac.PrimaryKey));
    //...
    return item;
}

So the result is not what i expected. I got the full list with 12000 items but every item is NULL. Why?

Image from the debugging ...

If I use another query like the following one, it works as I expected.

List<T> items = new List<T>(_Conn.Table<T>().Where<T>(ac => ac.PrimaryKey > 1000));

It seems there is an error in the implementation of select in SQLite-net, so calling _Conn.Table<T>().ToList() and then the select .Select(...) helps. sqlite-net issues

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM