码迷,mamicode.com
首页 > 移动开发 > 详细

Dapper链接查询扩展

时间:2015-04-28 17:44:48      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:

一对多映射关系

 /// <summary>
        /// 一对多连接查询
        /// </summary>
        /// <typeparam name="FirstT">主键表实体类型</typeparam>
        /// <typeparam name="SecondT">外键表实体类型</typeparam>
        /// <typeparam name="ReturnT">返回的实体类型(一般如果实体中存在映射关系的时候返回类型与主键表类型基本相同)</typeparam>
        /// <param name="sqlStr">连接查询的sql</param>
        /// <param name="ForeignKeyName">主键表实体中的的外键表集合名称</param>
        /// <param name="Name">外键表中的外键名</param>
        /// <returns></returns>
        public static List<FirstT> SelectOneToMore<FirstT, SecondT, ReturnT>(string sqlStr, string ForeignKeyName, string Name) where FirstT : IDataEntity, new()
        {
            using (var conn = ConnectionFactory.Connection)
            {
                List<FirstT> list = new List<FirstT>();
                var lookUp = new Dictionary<int, FirstT>();
                list = conn.Query<FirstT, SecondT, FirstT>(sqlStr,
                    (firstT, secondT) =>
                    {
                        FirstT t;
                        if (!lookUp.TryGetValue(firstT.ID, out t))
                        {
                            lookUp.Add(firstT.ID, t = firstT);
                        }
                        foreach (var item in firstT.GetType().GetProperties())
                        {
                            if (item.Name == ForeignKeyName)
                            {
                                var a = item.GetValue(firstT, null) as HashSet<SecondT>;
                                a.Add(secondT);
                            }
                        }
                        //t.GetType().GetMember(ForeignKeyName)
                        return firstT;
                    }, null, null, true, Name, null, null).ToList();

                return list;
            }



        }

一对一映射关系

 /// <summary>
        /// 一对一链接查询
        /// </summary>
        /// <typeparam name="FirstT">主键表类型</typeparam>
        /// <typeparam name="SecondT">外键表类型</typeparam>
        /// <typeparam name="ReturnT">返回值类型</typeparam>
        /// <param name="sqlStr">sql</param>
        /// <param name="ForeignKeyName">主键表实体中的的外键表集合名称</param>
        /// <param name="Name">外键表中的外键名</param>
        /// <returns></returns>
        public static List<FirstT> SelectOneToOne<FirstT, SecondT, ReturnT>(string sqlStr, string ForeignKeyName, string Name)
            where FirstT : IDataEntity, new()
            where SecondT : class,IDataEntity, new()
        {
            using (var conn = ConnectionFactory.Connection)
            {
                List<FirstT> list = new List<FirstT>();
                var lookUp = new Dictionary<int, FirstT>();
                list = conn.Query<FirstT, SecondT, FirstT>(sqlStr,
                    (firstT, secondT) =>
                    {
                        foreach (var item in firstT.GetType().GetProperties())
                        {
                            if (item.Name == ForeignKeyName)
                            {
                                item.SetValue(firstT, item.GetValue(firstT, null));
                            }
                        }
                        //t.GetType().GetMember(ForeignKeyName)
                        return firstT;
                    }, null, null, true, Name, null, null).ToList();

                return list;
            }

        }

 

Dapper链接查询扩展

标签:

原文地址:http://www.cnblogs.com/liuchang/p/4463431.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!