I use a combination of LINQ and Dapper in my work. I am replacing my LINQ code on Dapper with places for performance reasons. I have many LINQ data objects created by dragging and dropping into a Visual Studio SQL Server database diagram.
In the following example, I already have a LINQ object in memory, and I would like to pass it to Dapper as parameters for the query. For instance:
Animal animal = con.Query<Animal>(" select * " + " from animal " + " where animalid = @AnimalId " + " and animaltype = @AnimalType ", cagedAnimal).SingleOrDefault();
cagedAnimal contains the public properties AnimalId and AnimalType with getters and setters.
However, when I run this code, I get the following error:
Type: SMDApp.Models.Animal - this is not supported by dapper
The following code works:
Animal animal = con.Query<Animal>(" select * " + " from animal " + " where animalid = @AnimalId " + " and animaltype = @AnimalType ", new { AnimalId = cagedAnimal.AnimalId, AnimalType = cagedAnimal.AnimalType } ).SingleOrDefault();
It would be more convenient for me to use an existing object, especially when I use more than one property of the object as a parameter for the request. Can someone tell me why this works for an anonymous object, but not for automatically creating a LINQ object?
Edited in response to Ben Robinson's answer.
Edited a second time in response to Mark Gravell's answer.
source share