Goal:
Retrieve value from sql code of the datamember columns Asdf and Zxcv.
Problem:
I cannot retrieve the value of the datamember column Asdf and Zxcv.
I was not able to find the error and what part of the code am I missing?
Thank you!
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
and Zxcv
belong to Child
, but you split the resulting row with SchoolId
before those columns. You should change your splitOn
columns like this:
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();
This purpose of the splitOn
-parameter is this:
Your joined data comes as a resultset of rows and columns. Dapper doesn't know your datamodel, it can only map on column names or mappings you have defined. When you need to map to objects of multiple classes, you need to tell Dapper how to split the resultset into the various classes you specify. In your example you tell Dapper "My resultset contains Child
and Contact
", but Dapper needs a hint as to how to split the data. So you tell it that "ChildId" and "DropContactId" are the columns where the resultset is broken up. Dapper will then try to map the first part to Child
and the second part to Contact
. Note that "DropContactId" would have been enough, since the first resultset part is implied. Also note that splitOn
isn't necessary, if all your split columns are called "Id", then Dapper will do it automatically.