gpt4 book ai didi

.net - 嵌套多映射的 Dapper 语法

转载 作者:行者123 更新时间:2023-12-04 22:50:31 26 4
gpt4 key购买 nike

    class Person
{
Address Addr { get; set; }
int Age { get; set; }
}

class Address
{
string StreetName { get; set; }
County Cnty { get; set; }
}

class County
{
string CntyName;
string CntyCode;
}

这是我的存储过程,它从数据库中填充数据。
    Create Procedure SpGetAllPersons
As
Select CntyName, CntyCode, StreetName, Age from Persons

我试图在 dapper 查询下面写,但得到一个异常(exception)
    DBConn.Query<County, Address , Person, Person>
(DomainConstants.SpGetAllPersons,
(cnty, address, person) =>
{
address.Cnty = cnty;
person.Addr = address;
return person;
},
commandType: CommandType.StoredProcedure,
splitOn: "StreetName, Age").ToList();

我尝试使用以下概念,但它仅返回单个对象。我需要一份人员名单。
     var sql =
@"select
1 as PersonId, 'bob' as Name,
2 as AddressId, 'abc street' as Name, 1 as PersonId,
3 as Id, 'fred' as Name
";
var personWithAddress = connection.Query<Person, Address, Extra, Tuple<Person, Address, Extra>>
(sql, (p, a, e) => Tuple.Create(p, a, e), splitOn: "AddressId,Id").First();

提前致谢。

最佳答案

感谢马克和鲍勃跳上它。我能够以其他方式解决问题:

    DBConn.Query(DomainConstants.SpGetAllPersons, commandType: CommandType.StoredProcedure)
.Select(x => new Person
{
Addr = new Address
{
Cnty = new County
{
CntyName = x.CntyName,
CntyCode = x.CntyCode
},
StreetName = x.StreetName
},
Age = x.Age
}).ToList();

关于.net - 嵌套多映射的 Dapper 语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6855013/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com