問題描述
為什么我會收到此錯誤:
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.
這篇關于錯誤,LINQ to Entities 不支持方法的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!