Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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.. :)

like image 259
Sprintstar Avatar asked Nov 19 '25 08:11

Sprintstar


1 Answers

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.

like image 129
Mark Rendle Avatar answered Nov 21 '25 21:11

Mark Rendle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!