从 simple.data 枚举返回列表
我正在尝试处理来自 simple.data 的选择(查找..、全部等)的结果,但出现错误:
var db = Database.Open();
var codes = db.Code.All();
// 'object' does not contain a definition for 'First'
var firstcode = codes.First();
// 'object' does not contain a definition for 'ToList'
List<Code> codeList = codes.ToList();
codes
的类型为 {System.Linq.Enumerable .WhereSelectListIterator
。
我缺少什么?请有人添加一个 simple.data 标签..:)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
LINQ 方法不适用于从 db.Code.All() 返回的对象的主要原因是,在代码中的该点,C# 编译器不知道它是 IEnumerable,因此无法连接扩展方法。当然,C# 编译器不知道该对象是什么,因为它是动态的,因此它会忽略它并假设 First() 方法将在运行时解析。
我尝试在最近的版本中解决这个问题,并且支持许多方法,包括 ToList、First、FirstOrDefault、Single、SingleOrDefault 等。还有更多功能即将推出(在 0.9.x 版本中)。
让编译器恢复完全生效的最简单方法是显式指定类型而不是使用 var。例如,
将导致从 SimpleQuery 类型到 IEnumerable 类型的“隐式转换”(引用是因为它不是真正的,但它的作用类似),此时您可以开始再次对其使用 LINQ 方法。
The main reason that LINQ methods don't work on the object returned from db.Code.All() is that at that point in the code, the C# compiler doesn't know that it's an IEnumerable, so it can't hook up the extension methods. Of course, the C# compiler doesn't know what the object is, because it's dynamic, so it passes over it and assumes that the First() method will be resolved at runtime.
I've tried to address this in more recent releases, and many methods are supported, including ToList, First, FirstOrDefault, Single, SingleOrDefault and some others. Still more are to come soon (within the 0.9.x releases).
The easiest way to bring the compiler back into full effect is to explicitly specify the type instead of using var. For example
will cause an "implicit cast" (quoted because it's not really, but it acts like one) from the SimpleQuery type to the IEnumerable type, at which point you can start using LINQ methods on it again.
呵呵,简单的回答。我使用的是 https://github.com/markrendle/Simple.Data/downloads< 的最新版本/a> 但实际上它应该从 nuget http://nuget.org/List/Packages/Simple.Data.Core.. :(
Doh, simple answer. I was using the latest version from https://github.com/markrendle/Simple.Data/downloads but in fact it should installed from nuget http://nuget.org/List/Packages/Simple.Data.Core.. :(
看起来您需要一个
using System.Linq;
声明。如果实际的错误消息包含“object”一词,则表明从调用返回的
codes
类型是对象,而不是System.Linq.Enumerable.WhereSelectListIterator,Simple.Data.DynamicRecord>}
正如您所说,这将是错误的原因。It looks like you need a
using System.Linq;
declaration.If the actual error message contains the word 'object' then it indicates that the type of
codes
returned from your call is an object, not aSystem.Linq.Enumerable.WhereSelectListIterator<System.Collections.Generic.IDictionary<string,object>,Simple.Data.DynamicRecord>}
as you say, which would be the cause of the error.