错误,LINQ to Entities 不支持方法
- 作者: 绿植墙定制
- 来源: 51数据库
- 2022-12-08
问题描述
为什么我会收到此错误:
Why I am getting this error:
LINQ to Entities 不支持方法Single".考虑使用First"方法.
public ActionResult Details(int id)
Line 27: {
var result = (from d in _db.MovieSet
Line 29: where d.Id == id
Line 30: select d).Single();
//
//
}
代码编译安全,但只有在调用相应部分时才会中断.我是 LINQ 的新手,因此不知道哪些方法适用于 LINQtoSQL 或 LINQtoEntities.这意味着更多的错误!我们无法以这种方式记住所有方法.
Code compiles safe, but only breaks if call is made to the respective section. I am new to LINQ, therefore do not know which methods are for LINQtoSQL or LINQtoEntities. This means more errors! We cannot remember all methods this way.
我的问题是,如果适用于某些类型/场景的方法有限制,为什么它们会出现在 Intellisense 中?
My question is, if there are limitations to the methods applicable to certain types / scenarios, why do they appear in Intellisense?
任何有助于了解是否支持的解决方法/技术?
Any work-around / technique helpful to have an idea if one is supported ?
推荐答案
Microsoft 有一个完整的 Linq to Entities 中支持和不支持的方法列表.去那里可以找到这些信息.
Microsoft has a complete list of supported and unsupported methods in Linq to Entities. That's where to go to find out this information.
您会注意到 Single 和 SingleOrDefault 方法实际上在分页方法部分被列为不支持".
You'll notice that the Single and SingleOrDefault methods are in fact listed as "not supported" in the section on Paging Methods.
正如 Jared 所指出的,编译器在编译时不知道您使用的是哪个提供程序,因此它无法强制执行提供程序可能实现或可能不实现的扩展方法的编译时安全性.您将不得不依赖文档.
As Jared pointed out, the compiler does not know at compile time which provider you are using, so it has no way to enforce compile-time safety of extension methods that the provider may or may not implement. You'll have to rely on the documentation instead.
- C#通过fleck实现wss协议的WebSocket多人Web实时聊天(附源码)
- 团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
- 使用 MSBuild.exe 在发布模式下构建 C# 解决方案
- 当我发布 Web 应用程序时,AfterPublish 脚本不运行
- 构建时 T4 转换的产品仅在下一个构建中使用
- ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
- 新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
- 如何将条件编译符号(DefineConstants)传递给 msbuild
- MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
- NuGet 包还原找不到包,没有源
