无法检索字符串数据成员的值



Goal:
从数据成员列 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; }
}

AsdfZxcv属于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 "我的结果集包含ChildContact",但 Dapper 需要有关如何拆分数据的提示。因此,您告诉它"ChildId"和"DropContactId"是分解结果集的列。然后,Dapper 将尝试将第一部分映射到Child,将第二部分映射到Contact。请注意,"DropContactId"就足够了,因为第一个结果集部分是隐含的。另请注意,splitOn不是必需的,如果所有拆分列都称为"Id",则 Dapper 将自动执行此操作。

相关内容

最新更新