从 simple.data 枚举返回列表

发布于 2024-11-09 18:30:23 字数 579 浏览 8 评论 0 原文

我正在尝试处理来自 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.DynamicRecord>}

我缺少什么?请有人添加一个 simple.data 标签..:)

I am attempting to process results from a select (Find.., All, etc) from simple.data, but I am getting errors:

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();

The type of codes is {System.Linq.Enumerable.WhereSelectListIterator<System.Collections.Generic.IDictionary<string,object>,Simple.Data.DynamicRecord>}.

What am I missing? Someone add a simple.data tag please.. :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

葬﹪忆之殇 2024-11-16 18:30:23

LINQ 方法不适用于从 db.Code.All() 返回的对象的主要原因是,在代码中的该点,C# 编译器不知道它是 IEnumerable,因此无法连接扩展方法。当然,C# 编译器不知道该对象是什么,因为它是动态的,因此它会忽略它并假设 First() 方法将在运行时解析。

我尝试在最近的版本中解决这个问题,并且支持许多方法,包括 ToList、First、FirstOrDefault、Single、SingleOrDefault 等。还有更多功能即将推出(在 0.9.x 版本中)。

让编译器恢复完全生效的最简单方法是显式指定类型而不是使用 var。例如,

IEnumerable<Code> codes = db.Codes.All();

将导致从 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

IEnumerable<Code> codes = db.Codes.All();

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.

诠释孤独 2024-11-16 18:30:23

看起来您需要一个 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 a System.Linq.Enumerable.WhereSelectListIterator<System.Collections.Generic.IDictionary<string,object>,Simple.Data.DynamicRecord>} as you say, which would be the cause of the error.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文