.net-core - 无法检索字符串数据成员的值
问题描述
目标:
从数据成员列 Asdf 和 Zxcv 的 sql 代码中检索值。
问题:
我无法检索数据成员列 Asdf 和 Zxcv 的值。
我无法找到错误以及我缺少代码的哪一部分?
谢谢!
var data = new Dictionary<Guid, Child>();
var queryee2 = @"
SELECT
CONVERT(uniqueidentifier, 'c029f8be-29dc-41c1-8b38-737b4cc5a4df') AS ChildId,
1 AS SectionId,
1 as SchoolClassId,
1 as SchoolId,
'Daniel Dennett' AS Name,
'aa' as Asdf,
'bb' as Zxcv,
1 as DropByContactId,
'f' as Firstname,
'f' as Lastname,
1 as ContactId
UNION ALL
SELECT
CONVERT(uniqueidentifier, 'c029f8be-29dc-41c1-8b38-737b4cc5a4df') AS ChildId,
1 AS SectionId,
1 as SchoolClassId,
1 as SchoolId,
'Daniel Dennett' AS Name,
'aa' as Asdf,
'bb' as Zxcv,
0 as DropByContactId,
'f' as Firstname,
'f' as Lastname,
3 as ContactId
";
var ddfeedf = _db.Query<Child, Contact, Child>(queryee2, (child, contact) =>
{
//person
Child personEntity;
//trip
if (!data.TryGetValue(child.ChildId, out personEntity))
{
data.Add(child.ChildId, personEntity = child);
}
if (personEntity.Contacts == null)
{
personEntity.Contacts = new List<Contact>();
}
if (contact != null)
{
if (!personEntity.Contacts.Any(x => x.ContactId == contact.ContactId))
{
personEntity.Contacts.Add(contact);
}
}
return personEntity;
},
splitOn: "ChildId,SchoolId").Distinct();
public class Child
{
public Guid ChildId { get; set; }
public int SectionId { get; set; }
public int SchoolClassId { get; set; }
public int SchoolId { get; set; }
public string Name { get; set; }
public string Asdf { get; set; }
public string Zxcv { get; set; }
public ICollection<Contact> Contacts { get; set; }
}
public class Contact
{
public int DropByContactId { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
public int ContactId { get; set; }
}
解决方案
Asdf
并且Zxcv
属于Child
,但是您SchoolId
在这些列之前将结果行拆分。您应该像这样更改splitOn
列:
var ddfeedf = _db.Query<Child, Contact, Child>(queryee2, (child, contact) =>
{
//person
Child personEntity;
//trip
if (!data.TryGetValue(child.ChildId, out personEntity))
{
data.Add(child.ChildId, personEntity = child);
}
if (personEntity.Contacts == null)
{
personEntity.Contacts = new List<Contact>();
}
if (contact != null)
{
if (!personEntity.Contacts.Any(x => x.ContactId == contact.ContactId))
{
personEntity.Contacts.Add(contact);
}
}
return personEntity;
},
splitOn: "ChildId,DropByContactId").Distinct();
- 参数的目的splitOn
是:您的连接数据作为行和列的结果集出现。Dapper 不知道您的数据模型,它只能映射到您定义的列名或映射。当您需要映射到多个类的对象时,您需要告诉 Dapper 如何将结果集拆分为您指定的各种类。在您的示例中,您告诉 Dapper“我的结果集包含Child
和Contact
”,但 Dapper 需要关于如何拆分数据的提示。所以你告诉它“ChildId”和“DropContactId”是结果集被分解的列。然后,Dapper 将尝试将第一部分映射Child
到Contact
. 请注意,“DropContactId”就足够了,因为第一个结果集部分是隐含的。另请注意splitOn
不是必需的,如果您所有的拆分列都称为“Id”,那么 Dapper 会自动执行此操作。
推荐阅读
- quickfixj - 重置后是否需要重新订阅市场数据?
- node.js - axios 上传到 node.js API 导致 aws s3 中的文件变得不可读
- sql - 在databricks中使用sql创建最大日期函数
- javascript - 如何让 JavaScript 函数应用的类一直运行到窗口关闭(网页设计)
- python - 在有和没有包依赖的情况下进行测试
- javascript - 带有 Javascript 的 html 中的表格过滤器不起作用
- django - 从 Redis 丢失计划的 Celery 任务
- php - XML 输出未显示在浏览器中
- android-studio - 在 android studio 上运行 apk 时启动 studio 时出错
- ruby-on-rails - 在 active_storage_attachments 上设置 record_type 的 ActiveStorage 问题