public List <VM.ResourceInfoItem> GetResourceInfoItem(VM.ResourceInfoSearch searchModel) { //Build search criteria lambda expression Expression <Func <MD.tbl_Common_Resource, Boolean> > expr = BuildSearchCriteria(searchModel); //if not set the sort way ,then the default set will be used. string sortBy = "ResourceKey"; string sortDirection = "DESC"; if (!string.IsNullOrWhiteSpace(searchModel.SortBy)) { sortBy = searchModel.SortBy; sortDirection = searchModel.SortDirection; } IQueryable <MD.tbl_Common_Resource> resultQueryable; resultQueryable = this.Ctx.tbl_Common_Resource; if (expr != null) { resultQueryable = resultQueryable.Where(expr).SortWith(sortBy, sortDirection); } else { resultQueryable = resultQueryable.SortWith(sortBy, sortDirection); } var result = (from r in resultQueryable select new VM.ResourceInfoItem() { ResourceKey = r.ResourceKey, ResourceValueZHCN = r.ResourceValueZHCN, ResourceValueENUS = r.ResourceValueENUS, ResourceValueZHHK = r.ResourceValueZHHK }).ToList(); return(result); }
private Expression <Func <MD.tbl_Common_Resource, bool> > BuildSearchCriteria(VM.ResourceInfoSearch searchModel) { if (searchModel == null) { throw new ArgumentNullException("searchModel is null."); } Expression <Func <MD.tbl_Common_Resource, bool> > expr = null; DynamicLambda <MD.tbl_Common_Resource> builder = new DynamicLambda <MD.tbl_Common_Resource>(); if (searchModel.ResourceKey != null) { Expression <Func <MD.tbl_Common_Resource, bool> > temp = s => s.ResourceKey.Contains(searchModel.ResourceKey); expr = builder.BuildQueryAnd(expr, temp); } return(expr); }