linq - 无法使用 LINQ 语法编译我的项目

标签 linq c#-4.0 entity-framework-4

我一直在使用 Entity Framework,现在想用 LINQ 代码进行查询。我假设如果我从我的引用书“Entity Framework 4 In Action”中几乎逐字复制它,我的代码就可以编译。

这是我的代码:

from a in db.addresses
where a.accountId == 1
Select o;

出乎意料的是,intellisense 不接受我的任何代码。所以我做了一些研究并添加了项目引用、web.config 程序集引用和对 LINQ 的使用引用

using System.Data.Linq;

并编辑了 web.config 以包含

<assemblies>
<add assembly="System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>

即使如此,Intellisense 和编译器也不接受关键字“in”或 db.addresses(db 是上下文,在我的文件中的其他地方我可以毫无问题地进行如下调用:

users currentUser = db.users.Single(m => m.email == User.Identity.Name);

所以问题也不是我的数据库上下文。我搜索了堆栈、asp.net,并用谷歌搜索了“LINQ 语法未编译”,但找不到任何进一步的线索。

这是我的编译器错误:

Error 14 ; expected 147 20 AdamsStore

Error 15 ; expected 147 23 AdamsStore

Error 16 ; expected 148 23 AdamsStore

Error 17 ; expected 148 39 AdamsStore

Error 13 Invalid expression term 'in' 147 20 AdamsStore

Error 8 Only assignment, call, increment, decrement, and new object expressions can be used as a statement 147 18 AdamsStore

Error 10 Only assignment, call, increment, decrement, and new object expressions can be used as a statement 148 23 AdamsStore

Error 7 The type or namespace name 'from' could not be found (are you missing a using directive or an assembly reference?) 147 13 AdamsStore

Error 11 The type or namespace name 'select' could not be found (are you missing a using directive or an assembly reference?) 149 21 AdamsStore

最佳答案

  1. select需要小写
  2. 即使您已经using System.Data.Linq,您仍然需要using System.Linq

关于linq - 无法使用 LINQ 语法编译我的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12992033/

相关文章:

c# - 如何识别标记为级联删除的关联

c# - 使用 LINQ 将具有属性的 XML 转换为对象

c#-4.0 - 如何在MigraDoc表中设置单元格的背景色

c# - Entity Framework 数据上下文不包含使用继承的我的实体的集合。为什么?

C# LINQ 查询 (MYSQL EF) - 不同的最新记录

c#-4.0 - 我想使用 WebGrid 将自由格式的 native 查询输出发送到 excel

javascript - Linq JS 连接语法

Linq All()/Any() 但不为空

c# - 基于列以编程方式在共享点中创建 TreeView

mysql - 从 mysql 数据库操作系统大小 700Mb 中快速选择最后 n 条记录