2

我有一个最初XML用作数据源的项目。我现在正在重构代码以使用 SQLite ( System.Data.SQLite) 数据源。我找到了许多展示如何生成 SQL 查询的示例,但我更喜欢使用 LINQ,因为我更熟悉这种方法。

我最初使用带有 LINQ 表达式的 LINQpad 来测试我的 LINQ to Objects 查询。在 LINQpad 中,我创建了与数据库的连接以及基本模型信息。然后我像这样查询数据库:

var site = (from s in siteinfo
            where s.site_name == "some name"
            select new Site
            {
              ID = s.id,
              State = s.state,
              City = s.city,
              ...
            }
           );

LINQpad 查询识别我的数据库中的表和列,我能够生成我想要的对象。

移至 VS 2010 Express 我添加了System.Data.SQLiteSystem.Data.SQLite.Linq引用。然后我在数据源窗格中添加了到我的数据库的连接,其中包括文件connectionString中的a app.config

问题:应该将此连接作为数据集(我当前的方法)还是实体数据模型添加到我的项目中?我都试过了,但都没有回答下面的问题。

当我尝试使用 VS 从上面重新创建 LINQpad 查询时,智能感知没有给我选择特定表的选项。相反,我有sitedata.siteinfoDataTable其他表的选项和类似选项。例如:

var site = (from s in sitedataset.siteinfoDataTable
            where s.site_name == "some name"
            select new Site
            {
              ID = s.id,
              State = s.state,
              City = s.city,
              ...
            }
           );

我在参考中收到以下错误sitedataset.siteinfoDataTable

Could not find an implementation of the query pattern for source type 'sitedataset.siteinfoDataTable'. 'Where' not found. Are you missing a reference to 'System.Core.dll' or a using directive for 'System.Linq'?

我的项目确实引用了Core.dll并且我有一个using针对System.Linq.

我还尝试从数据集中查询不同的状态。例如:

var states = (from s in sitedataset.siteinfoDataTable
              select s.state).Distinct();

select语句产生以下错误:

Cannot convert lambda expression to type 'string' because it is not a delegate type

问题:使用 VS,我如何针对 SQLite 数据库编写 LINQ 查询?VS 中如何识别该数据库模式?

任何帮助表示赞赏,谢谢!

4

0 回答 0