예제 #1
0
파일: BaseCRUDVM.cs 프로젝트: zjmsky/WTM
        /// <summary>
        /// 根据主键获取Entity
        /// </summary>
        /// <param name="Id">主键Id</param>
        /// <returns>Entity</returns>
        protected virtual TModel GetById(object Id)
        {
            TModel rv = null;
            //建立基础查询
            var query = DC.Set <TModel>().AsQueryable();

            //循环添加其他设定的Include
            if (_toInclude != null)
            {
                foreach (var item in _toInclude)
                {
                    query = query.Include(item);
                }
            }
            if (typeof(TModel).IsSubclassOf(typeof(PersistPoco)))
            {
                var mod    = new IsValidModifier();
                var newExp = mod.Modify(query.Expression);
                query = query.Provider.CreateQuery <TModel>(newExp) as IOrderedQueryable <TModel>;
            }

            //获取数据
            rv = query.CheckID(Id).AsNoTracking().SingleOrDefault();
            if (rv == null)
            {
                throw new Exception("数据不存在");
            }
            //如果TopBasePoco有关联的附件,则自动Include 附件名称
            var fa = typeof(TModel).GetProperties().Where(x => x.PropertyType == typeof(FileAttachment)).ToList();

            foreach (var f in fa)
            {
                var fname = DC.GetFKName2 <TModel>(f.Name);
                var fid   = typeof(TModel).GetProperty(fname).GetValue(rv) as Guid?;
                var file  = DC.Set <FileAttachment>().Where(x => x.ID == fid).Select(x => new FileAttachment {
                    ID           = x.ID,
                    CreateBy     = x.CreateBy,
                    CreateTime   = x.CreateTime,
                    UpdateBy     = x.UpdateBy,
                    UpdateTime   = x.UpdateTime,
                    UploadTime   = x.UploadTime,
                    FileExt      = x.FileExt,
                    FileName     = x.FileName,
                    Length       = x.Length,
                    GroupName    = x.GroupName,
                    IsTemprory   = x.IsTemprory,
                    Path         = x.Path,
                    SaveFileMode = x.SaveFileMode
                }).FirstOrDefault();
                rv.SetPropertyValue(f.Name, file);
            }

            return(rv);
        }
예제 #2
0
        /// <summary>
        /// 进行搜索
        /// </summary>
        public virtual void DoSearch()
        {
            IOrderedQueryable <TModel> query = null;

            //根据搜索模式调用不同的函数
            switch (SearcherMode)
            {
            case ListVMSearchModeEnum.Search:
                query = GetSearchQuery();
                break;

            case ListVMSearchModeEnum.Export:
                query = GetExportQuery();
                break;

            case ListVMSearchModeEnum.Batch:
                query = GetBatchQuery();
                break;

            case ListVMSearchModeEnum.MasterDetail:
                query = GetMasterDetailsQuery();
                break;

            case ListVMSearchModeEnum.CheckExport:
                query = GetCheckedExportQuery();
                break;

            case ListVMSearchModeEnum.Selector:
                query = GetSelectorQuery();
                break;

            default:
                query = GetSearchQuery();
                break;
            }

            //如果设定了替换条件,则使用替换条件替换Query中的Where语句
            if (ReplaceWhere != null)
            {
                var mod    = new WhereReplaceModifier(ReplaceWhere);
                var newExp = mod.Modify(query.Expression);
                query = query.Provider.CreateQuery <TModel>(newExp) as IOrderedQueryable <TModel>;
            }
            if (string.IsNullOrEmpty(Searcher.SortInfo) == false)
            {
                var mod    = new OrderReplaceModifier(Searcher.SortInfo);
                var newExp = mod.Modify(query.Expression);
                query = query.Provider.CreateQuery <TModel>(newExp) as IOrderedQueryable <TModel>;
            }
            if (typeof(TModel).IsSubclassOf(typeof(PersistPoco)))
            {
                var mod    = new IsValidModifier();
                var newExp = mod.Modify(query.Expression);
                query = query.Provider.CreateQuery <TModel>(newExp) as IOrderedQueryable <TModel>;
            }
            if (PassSearch == false)
            {
                //如果需要分页,则添加分页语句
                if (NeedPage && Searcher.Limit != -1)
                {
                    //获取返回数据的数量
                    var count = query.Count();
                    if (count < 0)
                    {
                        count = 0;
                    }
                    if (Searcher.Limit == 0)
                    {
                        Searcher.Limit = ConfigInfo.RPP;
                    }
                    //根据返回数据的数量,以及预先设定的每页行数来设定数据量和总页数
                    Searcher.Count     = count;
                    Searcher.PageCount = (int)Math.Ceiling((1.0 * Searcher.Count / Searcher.Limit));
                    if (Searcher.Page <= 0)
                    {
                        Searcher.Page = 1;
                    }
                    if (Searcher.PageCount > 0 && Searcher.Page > Searcher.PageCount)
                    {
                        Searcher.Page = Searcher.PageCount;
                    }
                    EntityList = query.Skip((Searcher.Page - 1) * Searcher.Limit).Take(Searcher.Limit).AsNoTracking().ToList();
                }
                else //如果不需要分页则直接获取数据
                {
                    EntityList     = query.AsNoTracking().ToList();
                    Searcher.Count = EntityList.Count();
                    Searcher.Limit = EntityList.Count();
                    Searcher.Page  = 1;
                }
            }
            else
            {
                EntityList = query.AsNoTracking().ToList();
            }
            IsSearched = true;
            //调用AfterDoSearch函数来处理自定义的后续操作
            AfterDoSearcher();
        }