首页新闻找找看学习计划

代码优化问题

0
悬赏园豆:10 [已解决问题] 解决于 2016-02-22 14:40

 public List<DisplayShareViewModel> GetExportNewBaseList(ReportSearch search, out int total)
        {

            if (!search.Week.HasValue)
            {
                GregorianCalendar gc = new GregorianCalendar();
                search.Week = gc.GetWeekOfYear(DubaiTimeNow, CalendarWeekRule.FirstDay,

DayOfWeek.Monday);
            }           
           
             string [] strs=new string[8];
             var weeks = search.Weeks;
             int getYeas = search.Years.Value;
            //推算年
            
            if (search.Week.Value > 0 && search.Week.Value < 8)
            {
                getYeas -= 1; //当week=1 的时候向前推一年
            }            
            for (int i = 0; i < weeks.Count(); i++)
            {
                var Year = search.Years;
                if (search.Week < weeks[i])
                {
                    Year = getYeas;
                }               
                strs[i]=Year.ToString()+weeks[i];
                
            }
            var tbConvention = from c in db.TB_ConventionData
                               where strs.Contains(c.Year.ToString() + c.Week.ToString())
                               select c;           
            IQueryable<DisplayShareViewModel> query = from c in tbConvention
                                                      join s in db.TB_Store on c.StoreID

equals s.ID
                                                      join f in db.TB_Fields on c.FieldID

equals f.ID
                                                      join b in db.TB_BrandProduct on f.BPID

equals b.ID
                                                      join con in db.TB_Country on

s.CountryID equals con.ID
                                                      join r in db.TB_Retailer on

s.RetailerID equals r.ID
                                                      join m in db.TB_Module on f.ModuleID

equals m.ID
                                                      where m.ID == (int)ModuleEnum.Mobile ||

m.ID == (int)ModuleEnum.TAB
                                                      select new DisplayShareViewModel
                                                      {
                                                          Year = c.Year,
                                                          Week = c.Week,
                                                          Value = c.Data,
                                                          Brand = b.Name,
                                                          BrandId = b.ID,
                                                          Country = con.CountryName,
                                                          StoreCode = s.StoreCode,
                                                          StoreName = s.StoreName,
                                                          storeId = s.ID,
                                                          RetailerId = r.ID,
                                                          CountryId = con.ID,
                                                          Category = m.ID                     

                                    
                                                      };         

    


            #region
            if (search.CountryId.HasValue)
            {
                if (search.CountryId.Value > 0)
                {
                    query = query.Where(a => a.CountryId == search.CountryId.Value);
                }
            }
            if (search.Category.HasValue)
            {
                if (search.Category.Value > 0)
                {
                    query = query.Where(a => a.Category == search.Category);
                }
            }
            if (search.BrandId.HasValue)
            {
                if (search.BrandId.Value > 0)
                {
                    query = query.Where(a => a.BrandId == search.BrandId);
                }
            }
            if (!string.IsNullOrEmpty(search.StoreCode))
            {
                query = query.Where(a => a.StoreCode.ToUpper().Replace(" ", "").Contains

(search.StoreCode.Trim().Replace(" ", "").ToUpper()));
            }

            if (!string.IsNullOrEmpty(search.StoreName))
            {
                query = query.Where(a => a.StoreName.ToUpper().Replace(" ", "").Contains

(search.StoreName.Trim().Replace(" ", "").ToUpper()));
            }
            #endregion

            var result = query.OrderByDescending(c => c.StoreCode).ToList();

            total = result.GroupBy(w => new { w.storeId, w.Category, w.BrandId }).Count();
            if (search.IsExport)
            {
                search.PageSize = total;  //导出所有的数据
            }
            var resultGroupBy = result.GroupBy(w => new { w.storeId, w.Category, w.BrandId

}).Select(m => m).OrderByDescending(m => m.Key.storeId).Skip((search.PageIndex - 1) *

search.PageSize).Take(search.PageSize).ToList();
           // var resultGroupBy = result.GroupBy(w => new { w.storeId, w.Category, w.BrandId

});
            List<DisplayShareViewModel> displayShareViewModelList = new

List<DisplayShareViewModel>();      
             foreach (var Item in resultGroupBy)
             {
                 var displayShare = Item.FirstOrDefault();
                 DisplayShareViewModel DisplayShare = new DisplayShareViewModel();

                 DisplayShare.Year = displayShare.Year;
                 DisplayShare.Week = displayShare.Week;
                 DisplayShare.Brand = displayShare.Brand;
                 DisplayShare.BrandId = displayShare.BrandId;
                 DisplayShare.Country = displayShare.Country;
                 DisplayShare.StoreCode = displayShare.StoreCode;
                 DisplayShare.StoreName = displayShare.StoreName;
                 DisplayShare.Retailer = displayShare.Retailer;
                 DisplayShare.RetailerId = displayShare.RetailerId;
                 DisplayShare.CountryId = displayShare.CountryId;
                 DisplayShare.Category = displayShare.Category;
                 DisplayShare.storeId = displayShare.storeId;


                 for (int i = 0; i < weeks.Count(); i++)
                 {
                     //推算年
                     int Year = search.Years.Value;
                     if (search.Week < weeks[i])
                     {
                         Year = getYeas;
                     }
                     var list = result.Where(w => w.CountryId == DisplayShare.CountryId &&

w.storeId == DisplayShare.storeId && w.Category == DisplayShare.Category && w.BrandId ==

DisplayShare.BrandId && w.Week == weeks[i] && w.Year == Year);

                     var sum = 0;
                     if (list.Count() < 1)
                     {
                         SumValue modelcode = new SumValue();
                         modelcode.Week = weeks[i];
                         modelcode.Value += sum;                         
                         DisplayShare.SumValue.Add(modelcode);
                     }
                     else
                     {
                         var listCount = list.Count();
                         foreach (var Itemlist in list)
                         {
                             SumValue modelcode = new SumValue();
                             modelcode.Week = weeks[i];
                             sum = Convert.ToInt32(string.IsNullOrEmpty(Itemlist.Value) ? "0"

: Itemlist.Value);
                             modelcode.Value += sum;                             
                             DisplayShare.SumValue.Add(modelcode);
                         }
                     }
                 }
                 displayShareViewModelList.Add(DisplayShare);

             }

             return displayShareViewModelList;
           
            //return query.ToList();
        }

             求大神,指点优化这段代码,执行几千数据就卡的要死... 谢谢

IT小伙儿的主页 IT小伙儿 | 初学一级 | 园豆:11
提问于:2016-02-17 15:32
< >
分享
最佳答案
0

这个是实现啥的,算法需要修改才能提高效率

可能的关键修改地方:weeks如果大的话,需要调整;result这个这么多条件查询;list数目影响效率

VS有performance工具,采样看看那句的执行时间最长就可修改

收获园豆:10
2012 | 专家六级 |园豆:18877 | 2016-02-17 15:49

weeks 是一个数据 有8条数据,result要必须要多条件 ,我也不知道该怎么办了

IT小伙儿 | 园豆:11 (初学一级) | 2016-02-17 15:51

@IT小伙儿: result很多的话,换个结构,如Map等,找起来才能快,如果是List,一个个找肯定不行

2012 | 园豆:18877 (专家六级) | 2016-02-17 15:54

@2012: 不能使用MAP 吧 ,问题我有重新更新了一下,麻烦你看一下,谢谢.. 

IT小伙儿 | 园豆:11 (初学一级) | 2016-02-17 15:59
其他回答(1)
0

没前没后看不懂.应该是你2层循环的原因.

吴瑞祥 | 园豆:28675 (高人七级) | 2016-02-17 15:46

嗯嗯 是多层循环造成的 ,可是不知道怎么去优化了

支持(0) 反对(0) IT小伙儿 | 园豆:11 (初学一级) | 2016-02-17 15:48
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册