/// <summary>
        /// 将阅读器数据转换为实体列表
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public List <TEntity> DataTableToEntityList(SqlDataReader sdr)
        {
            //将阅读器数据转换成DataTable
            var       list  = AttributeHelper.GetEntityColumnAtrributes <TEntity>();
            DataTable dtTwo = new DataTable(tableName);

            foreach (var model in list)
            {
                DataColumn dc = new DataColumn();
                dc.ColumnName = model.propName;
                dc.DataType   = Type.GetType(model.typeName);
                dtTwo.Columns.Add(dc);
            }
            while (sdr.Read())
            {
                DataRow drTwo = dtTwo.NewRow();
                foreach (var model in list)
                {
                    try
                    {
                        if (sdr[model.fieldName] != null)
                        {
                            drTwo[model.propName] = sdr[model.fieldName];
                        }
                    }catch { }
                }
                dtTwo.Rows.Add(drTwo);
            }
            return(ConvertEntityByEmit.GetList <TEntity>(dtTwo));
        }
Example #2
0
        /// <summary>
        /// 表达式路由
        /// </summary>
        /// <param name="exp"></param>
        /// <returns></returns>
        string ExpressionRouter(System.Linq.Expressions.Expression exp)
        {
            //获取实体列的特性
            List <EntityPropColumnAttributes> columnAttrList = AttributeHelper.GetEntityColumnAtrributes <TEntity>();

            string sb = string.Empty;

            if (exp is System.Linq.Expressions.BinaryExpression)//二元运算符
            {
                System.Linq.Expressions.BinaryExpression be = ((System.Linq.Expressions.BinaryExpression)exp);
                return(BinarExpressionProvider(be.Left, be.Right, be.NodeType));
            }
            else if (exp is System.Linq.Expressions.MemberExpression)//成员
            {
                System.Linq.Expressions.MemberExpression me = ((System.Linq.Expressions.MemberExpression)exp);
                return(me.Member.Name);
            }
            else if (exp is System.Linq.Expressions.NewArrayExpression)//数组
            {
                System.Linq.Expressions.NewArrayExpression ae = ((System.Linq.Expressions.NewArrayExpression)exp);
                StringBuilder tmpstr = new StringBuilder();
                foreach (System.Linq.Expressions.Expression ex in ae.Expressions)
                {
                    tmpstr.Append(ExpressionRouter(ex));
                    tmpstr.Append(",");
                }
                return(tmpstr.ToString(0, tmpstr.Length - 1));
            }
            else if (exp is System.Linq.Expressions.MethodCallExpression)//方法
            {
                return(MethodExpression(exp));
            }
            else if (exp is System.Linq.Expressions.ConstantExpression)
            {
                System.Linq.Expressions.ConstantExpression ce = ((System.Linq.Expressions.ConstantExpression)exp);
                if (ce.Value == null)
                {
                    return("null");
                }
                else if (ce.Value is ValueType)
                {
                    return(ce.Value.ToString());
                }
                else if (ce.Value is string || ce.Value is DateTime || ce.Value is char)
                {
                    //return string.Format("'{0}'", ce.Value.ToString());
                    return(string.Format("{0}", ce.Value.ToString()));
                }
            }
            else if (exp is System.Linq.Expressions.UnaryExpression)
            {
                System.Linq.Expressions.UnaryExpression ue = ((System.Linq.Expressions.UnaryExpression)exp);
                return(ExpressionRouter(ue.Operand));
            }
            return(null);
        }
        /// <summary>
        /// 将数据表转换为实体列表
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public List <TEntity> DataTableToEntityList(DataTable dt)
        {
            if (dt == null)
            {
                return(new List <TEntity>());
            }
            var       list  = AttributeHelper.GetEntityColumnAtrributes <TEntity>();
            DataTable dtTwo = new DataTable(tableName);

            foreach (var model in list)
            {
                if (dt.Columns.Contains(model.fieldName))
                {
                    DataColumn dc = new DataColumn();
                    dc.ColumnName = model.propName;
                    dc.DataType   = dt.Columns[model.fieldName].DataType;
                    dtTwo.Columns.Add(dc);
                }
            }
            foreach (DataRow dr in dt.Rows)
            {
                DataRow drTwo = dtTwo.NewRow();
                foreach (var model in list)
                {
                    if (dt.Columns.Contains(model.fieldName))
                    {
                        drTwo[model.propName] = dr[model.fieldName];
                    }
                }
                dtTwo.Rows.Add(drTwo);
            }
            return(ConvertEntityByEmit.GetList <TEntity>(dtTwo));

            //List<TEntity> entityList = new List<TEntity>();
            //foreach (DataRow dr in dt.Rows)
            //{
            //    //实体
            //    TEntity entity = new TEntity();
            //    //循环属性
            //    foreach (PropertyInfo prop in entity.GetType().GetProperties())
            //    {
            //        //判断数据表中是否存在该列
            //        if (dt.Columns.Contains(prop.Name))
            //        {
            //            if (!string.IsNullOrEmpty(dr[prop.Name].ToStr()))
            //            {
            //                //Convert.ChangeType将对象转换为指定类型
            //                prop.SetValue(entity, Convert.ChangeType(dr[prop.Name], prop.PropertyType), null);
            //            }
            //        }

            //    }
            //    entityList.Add(entity);
            //}
            //return entityList;
        }
        /// <summary>
        /// 获取主键
        /// </summary>
        /// <returns></returns>
        public string GetPrimaryKey()
        {
            var list = AttributeHelper.GetEntityColumnAtrributes <TEntity>();

            foreach (var model in list)
            {
                if (model.isPrimaryKey)
                {
                    return(model.fieldName);
                }
            }
            return("");
        }
Example #5
0
        /// <summary>
        /// 二元运算符表达式
        /// </summary>
        /// <param name="left"></param>
        /// <param name="right"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        string BinarExpressionProvider(System.Linq.Expressions.Expression left, System.Linq.Expressions.Expression right, System.Linq.Expressions.ExpressionType type)
        {
            string where = "(";
            //先处理左边
            string leftStr = ExpressionRouter(left);
            //获取实体列的特性
            List <EntityPropColumnAttributes> columnAttrList = AttributeHelper.GetEntityColumnAtrributes <TEntity>();
            var list = columnAttrList.Where(w => w.propName == leftStr);

            if (list.Count() > 0)
            {
                EntityPropColumnAttributes columnAttribute = list.First();
                leftStr = columnAttribute.fieldName;
            }
            //节点类型
            string typeStr = ExpressionTypeCast(type);

            //再处理右边
            string rightStr = RightExpressionRouter(right).ToStr();

            where += leftStr;
            where += typeStr;

            if (rightStr == "null")
            {
                if (where.EndsWith(" ="))
                {
                    where = where.Substring(0, where.Length - 2) + " is null";
                }
                else if (where.EndsWith("<>"))
                {
                    where = where.Substring(0, where.Length - 2) + " is not null";
                }
            }
            else
            {
                //如果左侧包含(则代表左侧非字段
                if (leftStr.Contains("("))
                {
                    where += rightStr;
                }
                else
                {
                    int num = random.Next(100, 999);
                    where += "@" + leftStr + num;
                    listPara.Add(new SqlParameter("@" + leftStr + num, rightStr));
                }
            }
            return(where += ")");
        }
        public static DataTableEntityBuilder <T> CreateBuilder(DataRow dr)
        {
            DataTableEntityBuilder <T> dynamicBuilder = new DataTableEntityBuilder <T>();
            DynamicMethod method    = new DynamicMethod("DynamicCreateEntity", typeof(T), new Type[] { typeof(DataRow) }, typeof(T), true);
            ILGenerator   generator = method.GetILGenerator();
            LocalBuilder  result    = generator.DeclareLocal(typeof(T));

            generator.Emit(OpCodes.Newobj, typeof(T).GetConstructor(Type.EmptyTypes));
            generator.Emit(OpCodes.Stloc, result);
            var attributeList = AttributeHelper.GetEntityColumnAtrributes <T>();

            for (int i = 0; i < dr.ItemArray.Length; i++)
            {
                var list = attributeList.Where(w => w.fieldName.ToLower() == dr.Table.Columns[i].ColumnName.ToLower());
                if (list.Count() > 0)
                {
                    PropertyInfo pi         = typeof(T).GetProperty(list.First().propName);
                    Label        endIfLabel = generator.DefineLabel();
                    if (pi != null && pi.GetSetMethod() != null)
                    {
                        generator.Emit(OpCodes.Ldarg_0);
                        generator.Emit(OpCodes.Ldc_I4, i);
                        generator.Emit(OpCodes.Callvirt, isDBNullMethod);
                        generator.Emit(OpCodes.Brtrue, endIfLabel);
                        generator.Emit(OpCodes.Ldloc, result);
                        generator.Emit(OpCodes.Ldarg_0);
                        generator.Emit(OpCodes.Ldc_I4, i);
                        generator.Emit(OpCodes.Callvirt, getValueMethod);
                        generator.Emit(OpCodes.Unbox_Any, pi.PropertyType);
                        generator.Emit(OpCodes.Callvirt, pi.GetSetMethod());
                        generator.MarkLabel(endIfLabel);
                    }
                }
            }
            generator.Emit(OpCodes.Ldloc, result);
            generator.Emit(OpCodes.Ret);
            dynamicBuilder.handler = (Load)method.CreateDelegate(typeof(Load));
            return(dynamicBuilder);
        }
        /// <summary>
        /// 获取表达式列表对应的参数列表和条件
        /// </summary>
        /// <param name="express"></param>
        /// <returns></returns>
        public CoreFrameworkEntity GetParaListAndWhere(List <Expression> express)
        {
            //获取实体列的特性
            List <EntityPropColumnAttributes> columnAttrList = AttributeHelper.GetEntityColumnAtrributes <TEntity>();

            #region 获取参数和条件
            //参数列表
            List <SqlParameter> listPara = new List <SqlParameter>();
            //条件
            string where = "";
            if (express != null)
            {
                if (express.Count == 0)
                {
                    where = "1=1";
                }
                int i = 0;//运行的位置,从下标0开始
                foreach (Expression exp in express)
                {
                    string fieldName = columnAttrList.Where(w => w.propName.ToLower() == exp.FieldName.ToLower()).Count() > 0
                        ? columnAttrList.Where(w => w.propName.ToLower() == exp.FieldName.ToLower()).First().fieldName : exp.FieldName;
                    //连接符号
                    if (string.IsNullOrEmpty(exp.Join))
                    {
                        //判断个数是为了防止参数名相同
                        int    fieldCount = express.Where(e => e.FieldName == exp.FieldName).Count();
                        string paraName   = fieldCount == 1 ? exp.FieldName : exp.FieldName + "_p" + i.ToStr();

                        //当两个条件没有使用连接符时,则默认用and拼接,从第一个参数后试用
                        if (i > 0)
                        {
                            //如果上一个参数不是连接符时,默认每个参数是以and的形式拼接
                            if (string.IsNullOrEmpty(express[i - 1].Join))
                            {
                                where += " and ";
                            }
                        }

                        #region 条件语句
                        switch (exp.Condition.Trim())
                        {
                        case "like":
                            listPara.Add(new SqlParameter("@" + paraName, "%" + exp.Value + "%"));
                            where += " " + fieldName + " like @" + paraName;
                            break;

                        case "in":
                            where += " " + fieldName + " in (" + exp.Value + ")";
                            break;

                        default:
                            listPara.Add(new SqlParameter("@" + paraName, exp.Value));
                            where += " " + fieldName + " " + exp.Condition + " @" + paraName;
                            break;
                        }
                        #endregion
                    }
                    else
                    {
                        where += " " + exp.Join + " ";
                    }
                    i++;
                }
            }
            else
            {
                where = "1=1";
            }
            #endregion

            CoreFrameworkEntity entity = new CoreFrameworkEntity();
            entity.paraList = listPara;
            entity.where    = where;
            return(entity);
        }