for (int i = 1; i <= 12; i++)
            {
                foreach (var t in query)
                {
                    NetTrendByEmpActSpec.NetTrendByEmpActSpecRow dr = dt.NewNetTrendByEmpActSpecRow();
                    dr.RegionName = t.RegionName;
                    dr.TerritoryName = t.TerritoryID;
                    dr.TerritoryID = t.TerritoryName;
                    dr.DepartmentName = t.DepartmentName;
                    dr.Year = Condition.Time.Begin.Year.Value;
                    dr.Month = i;
                    dr.AccountID = t.AccountID;
                    dr.AccountLevel = t.AccountGrade;
                    dr.AccountName = t.AccountName;
                    dr.City = t.CityName;
                    dr.AccountType = t.AccountType;
                    dr.ProductName = t.ProductName;
                    dr.EmployeeID = t.EmployeeID;
                    dr.EmployeeName = t.EmployeeName;
                    dr.EmployeeState = t.Activate;
                    dr.EmployeeType = t.EmployeeType;
                    dr.Unit = string.Empty;
                    dr.Specification = string.Empty;
                    dr.Number = items.Where(x => x.Month == i && x.ProductID == t.ProductID && x.DepartmentID == t.ID && x.AccountID == t.AccountID && x.EmployeeID == t.EmployeeID).Sum(x => x.Number);
                    dr.Money = items.Where(x => x.Month == i && x.ProductID == t.ProductID && x.DepartmentID == t.ID && x.AccountID == t.AccountID && x.EmployeeID == t.EmployeeID).Sum(x => x.Money);
                    dt.Rows.Add(dr);
                }
            }上面是我的代码,for里面又套了个foreach,性能特别差,哪位高手能帮我指点一下,看看能不能把这段代码优化一下,小妹感激不尽!

解决方案 »

  1.   

    两种循环方式差别不是很大吧。
    你所谓的效率差,那就得看query的个数了。两层循环,也不会差到哪里去吧
      

  2.   

    外面的for循环是要拿出12个月的数据分组,里面的query现在有1万多条数据,我调试的时候就是这块代码比较慢,但是又想不出更好的方法来解决,看看大家有没有什么好的建议啊
      

  3.   

    是你数据量太大了用for或者forearch(两者几乎没有差别)肯定会很慢,大量的数据处理放在数据库里边吧
      

  4.   

                    foreach (var t in query) 
                    { 
                        NetTrendByEmpActSpec.NetTrendByEmpActSpecRow dr = dt.NewNetTrendByEmpActSpecRow(); 
                        dr.RegionName = t.RegionName; 
                        dr.TerritoryName = t.TerritoryID; 
                        dr.TerritoryID = t.TerritoryName; 
                        dr.DepartmentName = t.DepartmentName; 
                        dr.Year = Condition.Time.Begin.Year.Value; 
                        dr.AccountID = t.AccountID; 
                        dr.AccountLevel = t.AccountGrade; 
                        dr.AccountName = t.AccountName; 
                        dr.City = t.CityName; 
                        dr.AccountType = t.AccountType; 
                        dr.ProductName = t.ProductName; 
                        dr.EmployeeID = t.EmployeeID; 
                        dr.EmployeeName = t.EmployeeName; 
                        dr.EmployeeState = t.Activate; 
                        dr.EmployeeType = t.EmployeeType; 
                        dr.Unit = string.Empty; 
                        dr.Specification = string.Empty; 
                        dr.Number = items.Where(x => x.Month == i && x.ProductID == t.ProductID && x.DepartmentID == t.ID && x.AccountID == t.AccountID && x.EmployeeID == t.EmployeeID).Sum(x => x.Number); 
                        dr.Money = items.Where(x => x.Month == i && x.ProductID == t.ProductID && x.DepartmentID == t.ID && x.AccountID == t.AccountID && x.EmployeeID == t.EmployeeID).Sum(x => x.Money); 

    for (int i = 1; i <= 12; i++) 
    {
    dr.Month = i; 
    dt.Rows.Add(dr); 
    }
                    } 
    这么来吧
      

  5.   

    只要你foreach时query没有变化,就是说foreach执行过程中,query没有被另一个线程改变过的话,你的循环时间复杂度是死的,改不出太大的花来。
      

  6.   

    7楼的方法不行啊,我是要把数据放到DataSet里面,你的那个方法直接就报错了,主管提示我用多线程处理
    dr.Number = items.Where(x => x.Month == i && x.ProductID == t.ProductID && x.DepartmentID == t.ID && x.AccountID == t.AccountID && x.EmployeeID == t.EmployeeID).Sum(x => x.Number); 
    dr.Money = items.Where(x => x.Month == i && x.ProductID == t.ProductID && x.DepartmentID == t.ID && x.AccountID == t.AccountID && x.EmployeeID == t.EmployeeID).Sum(x => x.Money); 这样的求和的地方,我现在的程序中这样求和的地方特别多,但是我不懂那个多线程的处理,请大家给点建议
                        
      

  7.   

    那你们的意思就是没有好的方法来解决了吗?那怎么办啊?
    dr.Number = items.Where(x => x.Month == i && x.ProductID == t.ProductID && x.DepartmentID == t.ID && x.AccountID == t.AccountID && x.EmployeeID == t.EmployeeID).Sum(x => x.Number); 
    dr.Money = items.Where(x => x.Month == i && x.ProductID == t.ProductID && x.DepartmentID == t.ID && x.AccountID == t.AccountID && x.EmployeeID == t.EmployeeID).Sum(x => x.Money);像这样用到where的地方也很影响性能,大家还有没有更好的解决方案啊?帮帮小妹我吧。
      

  8.   

    主管告诉我说用多线程可以改善那个where的问题,但是我不会做啊,14楼的提高服务器性能是什么意思啊?能说的具体点吗?谢谢!