简体   繁体   English

从 simple.data 枚举返回列表

[英]Enumerating return list from simple.data

I am attempting to process results from a select (Find.., All, etc) from simple.data, but I am getting errors:我正在尝试处理来自 simple.data 的 select(查找..、全部等)的结果,但出现错误:

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>} . codes的类型是{System.Linq.Enumerable.WhereSelectListIterator<System.Collections.Generic.IDictionary<string,object>,Simple.Data.DynamicRecord>}

What am I missing?我错过了什么? Someone add a simple.data tag please.. :)请有人添加一个 simple.data 标签.. :)

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. LINQ 方法对从 db.Code.All() 返回的 object 不起作用的主要原因是,在代码中的那个点,ZD7EFA19FBE7D3972FD5ADB6024223D74Enumerable 编译器不知道它不能连接扩展方法。 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.当然,C# 编译器不知道 object 是什么,因为它是动态的,所以它通过它并假设 First() 方法将在运行时解析。

I've tried to address this in more recent releases, and many methods are supported, including ToList, First, FirstOrDefault, Single, SingleOrDefault and some others.我试图在最近的版本中解决这个问题,并且支持许多方法,包括 ToList、First、FirstOrDefault、Single、SingleOrDefault 和其他一些方法。 Still more are to come soon (within the 0.9.x releases).还有更多即将推出(在 0.9.x 版本中)。

The easiest way to bring the compiler back into full effect is to explicitly specify the type instead of using var.使编译器恢复完全有效的最简单方法是显式指定类型而不是使用 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.将导致从 SimpleQuery 类型到 IEnumerable 类型的“隐式转换”(引用,因为它不是真的,但它就像一个),此时您可以再次开始使用 LINQ 方法。

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 .. :(我使用的是https://github.com/markrendle/Simple.Data/downloads的最新版本,但实际上它应该从 nuget Z80791B3AE7002CB88C246876D9FAA8F8:/Z://.nugetData.org/List . (

It looks like you need a using System.Linq;看起来您需要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. 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>}正如你所说,这将是错误的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM