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,性能特别差,哪位高手能帮我指点一下,看看能不能把这段代码优化一下,小妹感激不尽!
{
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,性能特别差,哪位高手能帮我指点一下,看看能不能把这段代码优化一下,小妹感激不尽!
你所谓的效率差,那就得看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);
}
}
这么来吧
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); 这样的求和的地方,我现在的程序中这样求和的地方特别多,但是我不懂那个多线程的处理,请大家给点建议
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的地方也很影响性能,大家还有没有更好的解决方案啊?帮帮小妹我吧。