var sql = @"SELECT
a.id AS `Id`,
a.thing AS `Name`,
b.id AS `CategoryId`,
b.something AS `CategoryName`
FROM ..";
var products = connection.Query<Product, Category, Product>(sql,
(product, category) =>
{
product.Category = category;
return product;
},
splitOn: "CategoryId");
foreach(var p in products)
{
System.Diagnostics.Debug.WriteLine("{0} (#{1}) in {2} (#{3})", p.Name, p.Id, p.Category.Name, p.Category.Id);
}
Results in:
'First (#1) in (#0)'
'Second (#2) in (#0)'
CategoryId and CategoryName has values since the following
var products = connection.Query(sql).Select<dynamic, Product>(x => new Product
{
Id = x.Id,
Name = x.Name,
Category = new Category { Id = x.CategoryId, Name = x.CategoryName }
});
Results in:
'First (#1) in My Category (#10)'
'Second (#2) in My Category (#10)'
I'm connecting to a MySQL database if that has anything to do with it.
'CategoryName'
to just'Name'
? I was under the impression that Dapper would use some automapper convention to map'CategoryName'
(also tried'Category.Name'
) tox.Category.Name
– loraderon May 19 '11 at 13:07select * from Posts p join Authors a on p.AuthorId = a.Id
which is so succinct – Sam Saffron May 20 '11 at 4:55