Exemplo n.º 1
0
        private static IEnumerable <T> ExecuteArrayRowRead <T>(DbDataReader dr)
        {
            int[] indexes = null;
            var   list    = new List <T>();
            var   type    = typeof(T);
            var   props   = dicQueryTypeGetProperties.GetOrAdd(type, k => type.GetProperties());

            if (indexes == null)
            {
                var dic = new Dictionary <string, int>(StringComparer.CurrentCultureIgnoreCase);
                for (var a = 0; a < dr.FieldCount; a++)
                {
                    dic.Add(dr.GetName(a), a);
                }
                indexes = props.Select(a => dic.TryGetValue(a.Name, out var tryint) ? tryint : -1).ToArray();
            }
            list.Add((T)UtilsExpressionTree.ExecuteArrayRowReadClassOrTuple(type, indexes, dr, 0).Value);
            return(list);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 异步sql查询
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="facade"></param>
        /// <param name="sql"></param>
        /// <param name="parameterDic"></param>
        /// <returns></returns>

        public static async Task <IEnumerable <T> > SqlQueryAsync <T>(this DatabaseFacade facade, string sql, Dictionary <string, object> parameterDic) where T : class, new()
        {
            var      type  = typeof(T);
            List <T> ret   = new List <T>();
            var      props = dicQueryTypeGetProperties.GetOrAdd(type, k => type.GetProperties());

            int[] indexes = null;
            await ExecuteReaderAsync(dr =>
            {
                //ret = ExecuteArrayRowRead<T>(dr);
                if (indexes == null)
                {
                    var dic = new Dictionary <string, int>(StringComparer.CurrentCultureIgnoreCase);
                    for (var a = 0; a < dr.FieldCount; a++)
                    {
                        dic.Add(dr.GetName(a), a);
                    }
                    indexes = props.Select(a => dic.TryGetValue(a.Name, out var tryint) ? tryint : -1).ToArray();
                }
                ret.Add((T)UtilsExpressionTree.ExecuteArrayRowReadClassOrTuple(type, indexes, dr, 0).Value);
            }, facade, sql, parameterDic);

            return(ret.AsEnumerable());
        }