使用linq查询sqlite表会返回一个包含空项

时间:2017-06-28 12:34:11

标签: c# linq sqlite xamarin.forms

我在Xamarin Forms应用程序的sqlite数据库中有一个表,其中存储了大约12000个项目。当我尝试使用以下查询只返回一列时,我得到一个包含所有12000个值的列表,但列表的条目为空。

班级代码:

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; } }
}

我用以下行查询表:

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;
}

所以结果不是我的预期。我得到了12000个项目的完整列表,但每个项目都是NULL。为什么呢?

Image from the debugging ...

如果我使用其他查询,如下所示,它可以正常工作。

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

1 个答案:

答案 0 :(得分:1)

似乎在SQLite-net中执行select时出错,因此调用_Conn.Table<T>().ToList()然后选择.Select(...)会有所帮助。 sqlite-net issues

相关问题