Example #1
0
        public static string JoinTable(ReadOnlyCollection <ParameterExpression> parameters, params Type[] agrs)
        {
            string join = string.Empty;

            for (int i = 0; i < agrs.Length; i++)
            {
                Type   t         = agrs[i];
                string tableName = ModelDAL.GetTableName(t);
                if (parameters.Count > i)
                {
                    tableName = tableName + " as " + parameters[i];
                }

                if (i > 0)
                {
                    join = join + " left join " + tableName;
                }
                else
                {
                    join = tableName;
                }
            }

            return(join);
        }
        /// <summary>
        /// 实体列表转DataTable
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="modelList">实体列表</param>
        /// <returns></returns>
        public static DataTable ModelList2DataTable <T>(List <T> modelList)
        {
            if (modelList == null)
            {
                return(null);
            }
            DataTable dataTable = new DataTable();
            var       model     = modelList[0];
            Type      t         = model.GetType();//获得该类的Type

            dataTable.TableName = ModelDAL.GetTableName(t);
            //创建表结构、设置字段信息
            bool hasStar = false;

            foreach (PropertyInfo pi in t.GetProperties())
            {
                DataColumn     dataColumn = new DataColumn();
                ModelAttribute attr       = (ModelAttribute)Attribute.GetCustomAttribute(pi, typeof(ModelAttribute));// 属性值
                if (attr != null)
                {
                    dataColumn.ColumnName = attr.ColumnName;
                    dataColumn.DataType   = TypeUtil.GetType(attr.ColumnType);
                    dataColumn.Caption    = attr.ColumnTitle;
                    if (attr.IsPrimaryKey)
                    {
                        dataColumn.Unique = true;
                    }
                }
                else
                {
                    object value = pi.GetValue(model, null);

                    dataColumn.ColumnName = pi.Name;
                    if (value != null)
                    {
                        dataColumn.DataType = value.GetType();
                    }
                    //else
                    //{
                    //    dataColumn.DataType = typeof(string);
                    //}
                }
                if (Common.Star_SelectAllColumn.Equals(pi.Name))
                {
                    hasStar = true;
                    continue;
                }
                dataTable.Columns.Add(dataColumn);
            }

            //添加行数据
            foreach (var m in modelList)
            {
                object[] values = new object[t.GetProperties().Length - (hasStar ? 1 : 0)];
                int      idx    = 0;
                foreach (PropertyInfo pi in t.GetProperties())
                {
                    if (Common.Star_SelectAllColumn.Equals(pi.Name))
                    {
                        continue;
                    }
                    object         value = pi.GetValue(m, null);
                    ModelAttribute attr  = (ModelAttribute)Attribute.GetCustomAttribute(pi, typeof(ModelAttribute));// 属性值
                    //if (attr != null && !string.IsNullOrEmpty(attr.ColumnType))
                    //{
                    //    value = pi.GetValue(model, null);
                    //}
                    //else
                    //{
                    //    value = pi.GetValue(model, null);
                    //}
                    if (attr.ColumnType == ColumnType.datetimeType &&
                        (null == value || (System.DateTime)value == default(System.DateTime)))
                    {
                        //空时间类型或空值,不写入数据库
                        continue;
                    }
                    values[idx] = value;
                    idx++;
                }
                dataTable.Rows.Add(values);
            }

            return(dataTable);
        }