Example #1
0
        private KeyValuePair <string, List <string> > _GetEntityValue <TEntity>(TEntity param, List <string> columnString)
        {
            string key = "";

            List <string> entityValueList = new List <string>();

            foreach (PropertyInfo prop in param.GetType().GetProperties())
            {
                IgnoreAttribute ignoreAttr = prop.GetCustomAttributes <IgnoreAttribute>(false).FirstOrDefault();

                if (ignoreAttr != null)
                {
                    continue;
                }

                object value = prop.GetValue(param);

                if (value == null)
                {
                    continue;
                }

                if (value is DateTime)
                {
                    value = (Convert.ToDateTime(prop.GetValue(param).ToString())).ToString("yyyy-MM-dd HH:mm:ss");
                }
                else
                {
                    value = prop.GetValue(param).ToString();
                }

                KeyAttribute keyAttr = prop.GetCustomAttributes <KeyAttribute>(false).FirstOrDefault();

                if (keyAttr != null)
                {
                    key = string.Format("{0}='{1}'", _entityMapper.MapColumn(prop, columnString), value);
                }
                else
                {
                    entityValueList.Add(string.Format("{0}='{1}'", _entityMapper.MapColumn(prop, columnString), value));
                }
            }

            return(new KeyValuePair <string, List <string> >(key, entityValueList));
        }
Example #2
0
        private List <string> _GetEntityValues <TEntity>(IEnumerable <TEntity> param, List <string> columnString)
        {
            List <string> entityValueList = new List <string>();

            foreach (TEntity entity in param)
            {
                List <object> objectValues = new List <object>();

                PropertyInfo[] props = entity.GetType().GetProperties();

                foreach (string col in columnString)
                {
                    PropertyInfo prop = props.SingleOrDefault(p => p.Name == col);

                    if (prop == null)
                    {
                        prop = props.SingleOrDefault(p =>
                        {
                            ColumnNameAttribute collNameAttribute = p.GetCustomAttributes <ColumnNameAttribute>(false).FirstOrDefault();
                            if (collNameAttribute == null)
                            {
                                return(false);
                            }

                            return(collNameAttribute.ColumnName == col);
                        });
                    }

                    IgnoreAttribute ignoreAttr = prop.GetCustomAttributes <IgnoreAttribute>(false).FirstOrDefault();

                    if (ignoreAttr != null)
                    {
                        continue;
                    }

                    KeyAttribute keyAttr = prop.GetCustomAttribute <KeyAttribute>(false);

                    if (keyAttr != null)
                    {
                        DbOptionAttribute dbOptionAttr = prop.GetCustomAttribute <DbOptionAttribute>(false);

                        if (dbOptionAttr == null)
                        {
                            continue;
                        }

                        if (dbOptionAttr.Option == DbGenerateOption.AutoIncrement)
                        {
                            continue;
                        }
                    }

                    string value = "";

                    if (prop.GetValue(entity) is DateTime)
                    {
                        value = (Convert.ToDateTime(prop.GetValue(entity).ToString())).ToString("yyyy-MM-dd HH:mm:ss");
                    }
                    else
                    {
                        value = prop.GetValue(entity).ToString();
                    }

                    objectValues.Add(string.Format("'{0}'", MySqlHelper.EscapeString(value)));
                }

                entityValueList.Add(string.Format("({0})", string.Join(", ", objectValues)));
            }

            return(entityValueList);
        }