private List <TblContractHeader> GetContractHeader(int skip, int take, string sort, string filter, Dictionary <string, object> valuesObjects, int userIserial, out int fullCount, string company) { using (var context = new WorkFlowManagerDBEntities()) { context.CommandTimeout = 0; var defaultQuery = context.TblContractHeaders.Include( nameof(TblContractHeader.TblLkpSeason1)).Include( nameof(TblContractHeader.TblLkpBrandSection1)).Include( nameof(TblContractHeader.TblSubContractor1)); IQueryable <TblContractHeader> query; if (valuesObjects == null) { valuesObjects = new Dictionary <string, object>(); } var styleCodeFilter = valuesObjects.Where(o => o.Key.StartsWith("TblContractDetails_TblSalesOrderColor1_TblSalesOrder1_TblStyle1_StyleCode")); string styleCode = string.Empty; if (styleCodeFilter.Count() > 0) { styleCode = styleCodeFilter.ElementAt(0).Value.ToString().Replace("%", ""); styleCodeFilter.ToArray().ForEach(f => { valuesObjects.Remove(f.Key); filter = filter.Replace( string.Format("it.TblContractDetails.TblSalesOrderColor1.TblSalesOrder1.TblStyle1.StyleCode LIKE(@{0})", f.Key), ""); }); filter = filter.Trim(); if (filter.ToLower().StartsWith("and")) { filter = filter.Remove(0, 3); } if (filter.ToLower().EndsWith("and")) { filter = filter.Remove(filter.Length - 3 - 1, 3); } } if (!string.IsNullOrWhiteSpace(filter)) { filter = SharedOperation.GetBrandPerUser(filter, valuesObjects, userIserial, context) .Replace("it.Brand", "it.BrandCode").Replace("it.BrandCodeCode", "it.BrandCode"); var parameterCollection = SharedOperation.ConvertToParamters(valuesObjects); //var parameterCollection = valuesObjects.Select(valuesObject => new System.Data.Objects.ObjectParameter(valuesObject.Key, valuesObject.Value)).ToList(); fullCount = defaultQuery.Where(filter, parameterCollection.ToArray()).Count(); query = defaultQuery.Where(filter, parameterCollection.ToArray()).OrderBy(sort); } else { fullCount = defaultQuery.Count(x => x.TblLkpBrandSection1.TblUserBrandSections.Any( e => e.TblAuthUser == userIserial && e.BrandCode == x.BrandCode)); query = defaultQuery.OrderBy(sort).Where( x => x.TblLkpBrandSection1.TblUserBrandSections.Any( e => e.TblAuthUser == userIserial && e.BrandCode == x.BrandCode)); } List <TblContractHeader> result; if (!string.IsNullOrWhiteSpace(styleCode)) { query = query.Where(r => r.TblContractDetails.Any(d => d.TblSalesOrderColor1.TblSalesOrder1.TblStyle1.StyleCode.Contains(styleCode))); fullCount = query.Count(); try { // var sql = ((System.Data.Objects.ObjectQuery)query).ToTraceString(); //result = query.AsNoTracking().ToList(); result = query.ToList(); } catch (Exception ex) { throw; } } else { result = query.Skip(skip).Take(take).ToList(); } var db = new ccnewEntities(SharedOperation.GetSqlConnectionString(company)); foreach (var item in result) { item.Supplier = db.TBLsuppliers.FirstOrDefault(s => s.Iserial == item.SupplierIserial); item.Currency = db.TblCurrencyTests.FirstOrDefault(c => c.Iserial == item.TblCurrency); } return(result); } }