gpt4 book ai didi

c# - 小巧玲珑的简单贴图

转载 作者:太空狗 更新时间:2023-10-29 22:27:44 25 4
gpt4 key购买 nike

表:

create table Documents 
(Id int,
SomeText varchar(100),
CustomerId int,
CustomerName varchar(100)
)

insert into Documents (Id, SomeText, CustomerId, CustomerName)
select 1, '1', 1, 'Name1'
union all
select 2, '2', 2, 'Name2'

类:

public class Document
{
public int Id { get; set; }
public string SomeText { get; set; }
public Customer { get; set; }
}

public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
}

如何使用 Dapper 获取所有 Documents 及其 Customers?这给了我所有文件,但客户为空(当然):

connection.Query<Document>("select Id, SomeText, CustomerId, CustomerName from Documents")...

编辑 - 类似但更高级的映射问题:Dapper intermediate mapping

最佳答案

示例取自 dapper project page (参见多重映射部分):

var sql = 
@"select * from #Posts p
left join #Users u on u.Id = p.OwnerId
Order by p.Id";

var data = connection.Query<Post, User, Post>(sql, (post, user) => { post.Owner = user; return post;});
var post = data.First();

post.Content.IsEqualTo("Sams Post1");
post.Id.IsEqualTo(1);
post.Owner.Name.IsEqualTo("Sam");
post.Owner.Id.IsEqualTo(99);

关于c# - 小巧玲珑的简单贴图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10170882/

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