The old code I had before was atrociously slow but after some advice and research I was able to take a 2-5 minutes run time down to about 5-30 seconds. That is acceptable, but still looking to have it run as quickly as possible.
I'm still cleaning it up, like there shouldn't be two seperate PP entities calls.
public static List<DailyTeamGoal> GetListDailyTeamGoals(int teamId)
{
string teamGoal = "";
List<ProPit_User> lstProPit_User = new List<ProPit_User>();
using (PPEntities db = new PPEntities())
{
Team team = db.Teams.Where(x => x.teamID == teamId).FirstOrDefault();
if (team != null)
{
teamGoal = Convert.ToString(team.goal);
}
lstProPit_User = db.ProPit_User.Where(x => x.teamID == teamId).ToList();
}
List<DailyTeamGoal> lstDailyTeamGoal = new List<DailyTeamGoal>();
using (TEntities db = new TEntities())
using (PPEntities ppdb = new PPEntities())
{
//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 <= DateTime.Now.Day; day++)
{
DailyTeamGoal dtg = new DailyTeamGoal();
dtg.Date = day.ToString(); //dt.Month + "/" + day; + "/" + dt.Year;
dtg.TeamGoal = teamGoal;
//decimal orderTotalRep = 0m;
DateTime dtStartDate = Convert.ToDateTime(dt.Month + "/" + day + "/" + dt.Year);
DateTime dtEndDate = dtStartDate.AddDays(1);
var ordersQuery = db.Orders.Where(o => o.DateCompleted >= dtStartDate
&& o.DateCompleted <= dtEndDate
&& (o.Status == 1 || o.Status == 2)
&& o.Kiosk != 0).ToList();
var lstSalesRepIDs = ppdb.ProPit_User
.Where(x => x.teamID == teamId).Select(v => v.SalesRepID).ToList();
var total = (from o in ordersQuery
where lstSalesRepIDs.Contains(o.SalesRepID)
select o.OrderTotal).Sum();
orderTotal += total;
dtg.DailyTotal = orderTotal;
lstDailyTeamGoal.Add(dtg);
}
}
return lstDailyTeamGoal;
}
abs( o.DateCompleted - (dtStartDate + 0.5) ) < 0.5
\$\endgroup\$