I am building a new system that is using some tables from an old system.
For each user on this new system, I need to go to the old system and total up their sales. Currently it takes between 2-5 minutes to load.
public static List<DailyTeamGoal> GetListDailyTeamGoals(int teamId)
{
string teamGoal = "";
List<ProPit_User> lstProPit_User = new List<ProPit_User>();
using (ProsPitEntities db = new ProsPitEntities())
{
// Find the team
Team team = db.Teams.Where(x => x.teamID == teamId).FirstOrDefault();
if (team != null)
{
// Grab team goal
teamGoal = Convert.ToString(team.goal);
}
// Make a list of all users who are on the team
lstProPit_User = db.ProPit_User.Where(x => x.teamID == teamId).ToList();
}
List<DailyTeamGoal> lstDailyTeamGoal = new List<DailyTeamGoal>();
using (TEntities db = new TEntities())
{
//have to get every day of the month
DateTime dt = DateTime.Now;
int days = DateTime.DaysInMonth(dt.Year, dt.Month);
decimal orderTotal = 0m;
for (int day = 1; day <= days; day++)
{
// For every day in the month total the sales
DailyTeamGoal dtg = new DailyTeamGoal();
dtg.Date = day.ToString(); //dt.Month + "/" + day; + "/" + dt.Year;
dtg.TeamGoal = teamGoal;
decimal orderTotalRep = 0m;
foreach (var propit_User in lstProPit_User)
{
DateTime dtStartDate = Convert.ToDateTime(dt.Month + "/" + day + "/" + dt.Year);
DateTime dtEndDate = dtStartDate.AddDays(1);
var lstorderTotalRep = (from o in db.Orders
where o.DateCompleted >= dtStartDate
where o.DateCompleted <= dtEndDate
where (o.Status == 1 || o.Status == 2)
where o.Kiosk != 0
where o.SalesRepID == propit_User.SalesRepID
orderby o.OrderTotal descending
select o.OrderTotal).ToList();
foreach (var item in lstorderTotalRep)
{
//orderTotalRep =+ item;
orderTotalRep += item;
}
}
orderTotal += orderTotalRep;
dtg.DailyTotal = orderTotal;
lstDailyTeamGoal.Add(dtg);
}
}
return lstDailyTeamGoal;
}
The above code will use my site to find the teamID
the logged in person is on. It will then find all members who are on that team. For each member it finds it will calculate the total sales and then spit me back a list. Any way to speed this up?
Orders
including columnsDateCompleted
,Status
,Kiosk
andSalesRepID
? How many rows are we talking about? Have you profiled anything to see where the bottleneck might be? \$\endgroup\$