【问题标题】:Why my Linq query against SQLite does not return all table field values为什么我对 SQLite 的 Linq 查询不返回所有表字段值
【发布时间】:2015-06-20 15:14:43
【问题描述】:

我正在使用 SQLite 构建一个 WinRT 应用程序。

我在我的 C# 代码中运行以下方法,以便从表 ProductSubCategory 中检索具有相同父 ID 的所有列

我正在使用最新版本的 SQLite.Net 和 SQLITE.NET.ASync

public async Task<IEnumerable<ProductSubCategory>> GetProductSubCategoryAsync(int ParentCategoryId)
{
    List<ProductSubCategory> lst = new List<ProductSubCategory>();
    var DBconn = await _sqliteService.OpenSQLiteConnection();
    {
        //retrive test data
        lst = await DBconn.Table<ProductSubCategory>().Where(c=>c.ParentSubCategoryId==ParentCategoryId).ToListAsync();
    }
    return lst;
}

类定义:

public class ProductCategory:ObservableObject
{
    [PrimaryKey]
    public int ProductCategoryId { get; set; }

    public string Name { get; set; }
    public string RowGuid { get; set; }
    public DateTime ModifiedDate { get; set; }
}

当代码执行时,它会根据过滤器返回正确数量的记录,但并非所有返回字段值都被返回。

例如,我的所有 INT 类型的表字段以及主键都不会在我的对象中更新。

有什么理由和解决方案让它发挥作用吗?我需要检索所有这些列数据。

这是否取决于创建字段的方式?

【问题讨论】:

  • 请添加ProductSubCategory的类定义。
  • productSubCategory 类定义存在相同的文件名和类型。我在上面的帖子中添加了定义。我需要在我的类字段中返回 PrimaryKey,但事实并非如此。有什么理由吗?

标签: c# linq sqlite windows-runtime sqlite-net


【解决方案1】:

我设法找出问题所在。 我的未返回数据的表数据库列字段名为 ProductCategoryID,我的对象名称为 ProductCategoryId

这意味着何时从数据库中获取字段,字段名称区分大小写,但奇怪的是,如果您插入数据,它们不区分大小写。

我只是将表字段名称重命名为 ProductCategoryID,现在我可以正确获取数据

【讨论】:

    猜你喜欢
    • 2023-03-09
    • 1970-01-01
    • 2017-07-07
    • 2011-07-24
    • 1970-01-01
    • 1970-01-01
    • 2021-10-26
    • 2021-12-02
    • 2016-11-01
    相关资源
    最近更新 更多