Example #1
0
        public async Task <List <T> > SimpleSqlQueryAsync <T>(SimpleSQLQueryModel queryModel) where T : class, new()
        {
            var result = await client.Sql.QueryAsync(t => t.FetchSize(queryModel.FetchSize).Format(queryModel.Format.ToString()).Query(queryModel.Sql)).ConfigureAwait(false);

            var      rows       = result.Rows.ToList();
            var      colunms    = result.Columns.ToList();
            List <T> entityList = new List <T>();

            GetDebugInfo(result);
            for (int i = 0; i < rows.Count; i++)
            {
                var row    = rows[i];
                T   entity = new T();
                for (int j = 0; j < colunms.Count; j++)
                {
                    var column = colunms[j];
                    var val    = row[j];
                    if (!column.Name.Contains("."))
                    {
                        var emitSetter = EmitHelper.EmitSetter <T>(column.Name);
                        emitSetter(entity, GetValue(column, val));
                    }
                    else
                    {
                        var type = typeof(T);
                        var pop  = type.GetProperties(BindingFlags.Instance | BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.NonPublic);
                        for (int n = 0; n < pop.Length; n++)
                        {
                            string childClass = column.Name.Split('.')[0];
                            string childField = column.Name.Split('.')[1];
                            object childValue = GetValue(column, val);
                            if (pop[n].Name.ToLower() == childClass.ToLower())
                            {
                                var childType        = pop[n].PropertyType;
                                var childEntityClass = childType.Assembly.CreateInstance(childType.FullName, true);
                                var popchilds        = childType.GetProperties(BindingFlags.Instance | BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.NonPublic);
                                var popchild         = popchilds.First(t => t.Name.ToLower() == childField.ToLower());
                                var emitSetter       = EmitHelper.CreatePropertiesFunc(popchilds);
                                popchild.SetValue(childEntityClass, childValue);
                                pop[n].SetValue(entity, childEntityClass);
                            }
                        }
                    }
                }
                entityList.Add(entity);
            }
            return(entityList);
        }
Example #2
0
 private void GetChildProperty(object entity, PropertyInfo[] propertyInfos, object property, SqlColumn column, SqlValue val)
 {
     for (int n = 0; n < propertyInfos.Length; n++)
     {
         //A.B.C 最后一个C是属性,前面的是类
         var    classInfo       = column.Name.Split('.');
         string childFirstClass = classInfo[0];
         string childField      = classInfo[classInfo.Length - 1];
         object childValue      = GetValue(column, val);
         if (classInfo.Length > 2)
         {
             for (int m = 0; m < classInfo.Length - 1; m++)
             {
                 string childClass1 = classInfo[m];
                 if (propertyInfos[n].Name.ToLower() == childClass1.ToLower())
                 {
                     var childType        = propertyInfos[n].PropertyType;//myclass
                     var childEntityClass = childType.Assembly.CreateInstance(childType.FullName, true);
                     var popchilds        = childType.GetProperties(BindingFlags.Instance | BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.NonPublic);
                     propertyInfos[n].SetValue(entity, childEntityClass);
                     GetChildProperty(childEntityClass, popchilds, property, column, val);
                 }
             }
         }
         else
         {
             if (propertyInfos[n].Name.ToLower() == childFirstClass.ToLower())
             {
                 var childType        = propertyInfos[n].PropertyType;
                 var childEntityClass = childType.Assembly.CreateInstance(childType.FullName, true);
                 var popchilds        = childType.GetProperties(BindingFlags.Instance | BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.NonPublic);
                 var popchild         = popchilds.First(t => t.Name.ToLower() == childField.ToLower());
                 var emitSetter       = EmitHelper.CreatePropertiesFunc(popchilds);
                 popchild.SetValue(childEntityClass, childValue);
                 propertyInfos[n].SetValue(entity, childEntityClass);
             }
         }
     }
 }