コード例 #1
0
        private List <ErrCell> ValidateMatching <T>(List <CellMatching <T> > rowValidates, int startRowIndex)
        {
            if (rowValidates == null)
            {
                return(new List <ErrCell>());
            }

            foreach (var rowValidate in rowValidates)
            {
                PropertyExpressionParser <T> property = new PropertyExpressionParser <T>(rowValidate.paramater);
                if (!_propertyCollection.Any(u => u.Key == property.Name))
                {
                    throw new Exception(string.Format("列必须拥有映射关系{0}", property.Name));
                }
            }

            var excel = new ExcelQueryFactory(FilePath);

            foreach (var mapping in PropertyMappings)
            {
                excel.AddMapping(mapping.Key, mapping.Value);
            }
            var rows = (from c in excel.Worksheet <T>(SheetIndex)
                        select c).ToList();

            List <ErrCell> errCells = new List <ErrCell>();

            foreach (var row in rows)
            {
                errCells.AddRange(GetErrCellByMatching(row, startRowIndex++, rowValidates));
            }
            return(errCells);
        }
コード例 #2
0
        public void FromReadonly_GetOptionInfo_ThrowsException()
        {
            var parser = new PropertyExpressionParser();

            var error = Should.Throw <ArgumentException>(() => parser.GetOptionInfo <TestOptions, bool>(p => p.IsValid));

            error.Message.ShouldContain("Property IsValid is readonly.");
        }
コード例 #3
0
        public void FromPath_GetOptionInfo_SetsTargetType()
        {
            var parser = new PropertyExpressionParser();

            var info = parser.GetOptionInfo <TestOptions, string>(p => p.Path);

            info.TargetType.ShouldBe(typeof(string));
        }
コード例 #4
0
        public void FromPath_GetOptionInfo_SetsName()
        {
            var parser = new PropertyExpressionParser();

            var info = parser.GetOptionInfo <TestOptions, string>(p => p.Path);

            info.Name.ShouldBe("Path");
        }
コード例 #5
0
        public void AddMapping <T>(Expression <Func <T, object> > propertyExpression, string excelColName)
        {
            PropertyExpressionParser <T> property = new PropertyExpressionParser <T>(propertyExpression);
            string propertyName = property.Name;

            if (PropertyMappings.ContainsKey(propertyName))
            {
                PropertyMappings[propertyName] = excelColName;
            }
            else
            {
                PropertyMappings.Add(propertyName, excelColName);
            }
        }
コード例 #6
0
        public Model GetModel(Model condition, params Expression <Func <Model, object> >[] ignorePropertyExpressions)
        {
            string sWhere     = " 1=1 ";
            var    parameters = new List <object>();

            for (int i = 0; i < ignorePropertyExpressions.Length; i++)
            {
                var ignorePropertyExpression = ignorePropertyExpressions[i];
                var item = new PropertyExpressionParser <Model>(condition, ignorePropertyExpression);
                sWhere += $" and {item.Name} = @{i} ";
                parameters.Add(item.Value);
            }

            var model = Db.Sql(@"select * from " + TableName + " where " + sWhere, parameters.ToArray()).QuerySingle <Model>();

            return(model);
        }
コード例 #7
0
        /// <summary>
        /// 4.x扩展修改方法(把不须要修改的列用LAMBDA数组表示出来)
        /// </summary>
        /// <param name="model">要修改的实体对象</param>
        /// <param name="ignoreProperties">不须要修改的相关字段</param>
        /// <returns>受影响的行数</returns>
        public int UpdateEntity(T model, params Expression <Func <T, object> >[] ignoreProperties)
        {
            int iret = -1;

            //Logger("修改" + typeof(T).Name + "中的数据", () =>
            //{
            using (DbContext db = new Model.Entities()) //DBContextFactory().GetDbContext()
            {
                db.Entry <T>(model).State = EntityState.Detached;
                db.Set <T>().Attach(model);

                DbEntityEntry entry = db.Entry <T>(model);
                entry.State = System.Data.EntityState.Unchanged;

                Type t = typeof(T);
                List <PropertyInfo> proInfos = t.GetProperties(BindingFlags.Instance | BindingFlags.Public).ToList();

                Dictionary <string, PropertyInfo> dicPros = new Dictionary <string, PropertyInfo>();
                proInfos.ForEach(
                    p => dicPros.Add(p.Name, p)
                    );

                if (ignoreProperties != null)
                {
                    foreach (var ignorePropertyExpression in ignoreProperties)
                    {
                        //根据表达式得到对应的字段信息
                        var ignorePropertyName = new PropertyExpressionParser <T>(ignorePropertyExpression).Name;
                        dicPros.Remove(ignorePropertyName);
                    }
                }

                foreach (string proName in dicPros.Keys)
                {
                    entry.Property(proName).IsModified = true;
                }
                iret = db.SaveChanges();
            }
            return(iret);
        }
コード例 #8
0
        private List <ErrCell> GetErrCellByMatching <T>(T box, int rowIndex, List <CellMatching <T> > rowValidates)
        {
            List <T> row = new List <T>();

            row.Add(box);

            List <ErrCell> errCells = new List <ErrCell>();

            for (int i = 0; i < rowValidates.Count; i++)
            {
                var rowValidate = rowValidates[i];
                if (!row.Any(rowValidate.matchCondition))
                {
                    string errMsg = rowValidate.errMsg;

                    PropertyExpressionParser <T> property = new PropertyExpressionParser <T>(rowValidate.paramater);
                    var colDescription = (Attribute.GetCustomAttribute(property.GetPropertyInfo(rowValidate.paramater), typeof(ExcelColumnAttribute))) as ExcelColumnAttribute;
                    int ColIndex       = _propertyCollection[property.Name].ColIndex;

                    if (!errCells.Any(u => u.RowIndex == rowIndex && u.ColumnIndex == ColIndex))
                    {
                        errCells.Add(new ErrCell()
                        {
                            RowIndex    = rowIndex,
                            ColumnIndex = ColIndex,
                            Name        = RowValidate.GetCellStation(rowIndex, ColIndex),
                            ErrMsg      = errMsg
                        });
                    }
                    else
                    {
                        errCells.Find(u => u.RowIndex == rowIndex && u.ColumnIndex == ColIndex).ErrMsg += ";" + errMsg;
                    }
                }
            }
            return(errCells);
        }