首页 > 解决方案 > 无法检索字符串数据成员的值

问题描述

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

标签: .net-coreormdapper

解决方案


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


推荐阅读