给出两个实体类,一对多的关系
public class UserDto
{
public int UserId { get; set; }
public string Name { get; set; }
public List<CarDto> Cars { get; set; }
}
public class CarDto
{
public int UserId { get; set; }
public string Make { get; set; }
public string Color { get; set; }
}
在需要一对多查询的时候,使用如下方式进行查询
v2是NPoco是2.x的版本,v3是NPoco是3.x的版本。
查询的时候要带上条件,不然会把CarDto的所有数据都给查出来了
IDatabase db = new Database("connStringName");
//v2
var users = db.FetchOneToMany<UserDto, CarDto>(x => x.UserId,
"select u.*, c.* from Users u inner join Cars c on u.UserId = c.UserId order by u.UserId");
//v3
var users = db.FetchOneToMany<UserDto>(x => x.Cars,
"select u.*, c.* from Users u inner join Cars c on u.UserId = c.UserId order by u.UserId");