Example #1
0
        /// <summary>
        /// Many to many (N -> *) multi-map.
        /// Load a list of children to a parent's property.
        /// Example:
        /// SELECT Parent.ID AS ParentId, TC.* FROM TableChild TC
        /// INNER JOIN
        /// (
        ///     SELECT ID From TableParent
        /// ) Parent ON Parent.ID = TC.ParentID
        ///
        /// </summary>
        public static Dictionary <TParentKey, IList <TReturn> > ReadChild <TParentKey, TReturn>(this SqlMapper.GridReader reader, Type[] typesChild, Func <object[], TReturn> mapChild, string splitOn = "Id", bool buffered = true)
        {
            Dictionary <TParentKey, IList <TReturn> > childMap = new Dictionary <TParentKey, IList <TReturn> >();

            typesChild.Append(typeof(DapperManyToMany <TParentKey, TReturn>));

            reader.Read(typesChild, objects =>
            {
                DapperManyToMany <TParentKey, TReturn> wrapper = (DapperManyToMany <TParentKey, TReturn>)objects.Last();

                object[] newArray = new object[objects.Length - 1];
                Array.Copy(objects, 0, newArray, 0, newArray.Length);

                wrapper.Child = mapChild(newArray);

                if (childMap.ContainsKey(wrapper.ParentId))
                {
                    childMap[wrapper.ParentId].Add(wrapper.Child);
                }
                else
                {
                    childMap.Add(wrapper.ParentId, new List <TReturn>
                    {
                        wrapper.Child
                    });
                }

                return(wrapper.Child);
            }, splitOn, buffered);
            return(childMap);
        }
Example #2
0
        /// <summary>
        /// Many to many (N -> *) multi-map.
        /// Load a list of children to a parent's property.
        /// Example:
        /// SELECT Parent.ID AS ParentId, TC.* FROM TableChild TC
        /// INNER JOIN
        /// (
        ///     SELECT ID From TableParent
        /// ) Parent ON Parent.ID = TC.ParentID
        ///
        /// </summary>
        public static Dictionary <TParentKey, IList <TReturn> > ReadChild <TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh, T8, T9, T10, T11, T12, T13, T14, T15, T16, TReturn, TParentKey>(this SqlMapper.GridReader reader, Func <TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh, T8, T9, T10, T11, T12, T13, T14, T15, T16, TReturn> func, string splitOn = "Id", bool buffered = true)
        {
            Type[] types =
            {
                typeof(TFirst),
                typeof(TSecond),
                typeof(TThird),
                typeof(TFourth),
                typeof(TFifth),
                typeof(TSixth),
                typeof(TSeventh)
                ,               typeof(T8)
                ,               typeof(T9)
                ,               typeof(T10)
                ,               typeof(T11)
                ,               typeof(T12)
                ,               typeof(T13)
                ,               typeof(T14)
                ,               typeof(T15)
                ,               typeof(T16)
                ,               typeof(DapperManyToMany <TParentKey, TReturn>)
            };

            Dictionary <TParentKey, IList <TReturn> > childMap = new Dictionary <TParentKey, IList <TReturn> >();

            reader.Read(types, objects =>
            {
                DapperManyToMany <TParentKey, TReturn> wrapper = (DapperManyToMany <TParentKey, TReturn>)objects[16];

                wrapper.Child = func((TFirst)objects[0], (TSecond)objects[1], (TThird)objects[2], (TFourth)objects[3], (TFifth)objects[4], (TSixth)objects[5], (TSeventh)objects[6], (T8)objects[7], (T9)objects[8], (T10)objects[9], (T11)objects[10], (T12)objects[11], (T13)objects[12], (T14)objects[13], (T15)objects[14], (T16)objects[15]);

                if (childMap.ContainsKey(wrapper.ParentId))
                {
                    childMap[wrapper.ParentId].Add(wrapper.Child);
                }
                else
                {
                    childMap.Add(wrapper.ParentId, new List <TReturn>
                    {
                        wrapper.Child
                    });
                }

                return(wrapper.Child);
            }, splitOn, buffered);
            return(childMap);
        }