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);
}
结果是:
'First (#1) in (#0)'
'Second (#2) in (#0)'
CategoryId 和 CategoryName 具有以下值
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 }
});
结果是:
'First (#1) in My Category (#10)'
'Second (#2) in My Category (#10)'
如果这与它有任何关系,我将连接到 MySQL 数据库。