2

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

casperOne
  • 73,706
  • 19
  • 184
  • 253
Sprintstar
  • 7,938
  • 5
  • 38
  • 51

3 Answers3

6

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.

Mark Rendle
  • 9,274
  • 1
  • 32
  • 58
2

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

Sprintstar
  • 7,938
  • 5
  • 38
  • 51
1

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.

George Duckett
  • 31,770
  • 9
  • 95
  • 162