用户登录
用户注册

分享至

错误,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.

软件
前端设计
程序设计
Java相关