Search code examples
simple.data

Magic Casting to collection of results


In the Simple.Data examples, there is an example of 'Magic Casting':

// When you assign the dynamic record to a static type, any matching properties are auto-mapped.
var db = Database.Open();
Customer customer = db.Customers.FindByCustomerId(1);

Does Simple.Data also magically cast if there are multiple records returned? Something like this:

var db = Database.Open();
IEnumerable<Customer> customers = db.Customers.FindBySurname("Smith");

Obviously I have tried the above and it doesn't work ("Cannot implicitly convert type" from SimpleQuery to my concrete type). Any advice would be welcome.


Solution

  • FindBySurname returns a single record. If you use FindAllBySurname you'll get an enumerable, which should magic cast OK. (If for some reason it doesn't, you can call .Cast() on it.)