无法检索字符串数据成员的值
Cannot retrieve value to the string datamember
目标:
从数据成员列 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 会自动执行此操作。
目标:
从数据成员列 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 会自动执行此操作。