On my machine it runs in about 60 seconds and about 2-3 minutes live.
The criteria needed are:
- Current month only (from first of month until current day)
- I need the rank of the currently logged in person based on total sales.
I have two tables across two databases where this data is to be gathered:
- Database 1:
ProPit_User
table (this houses the logged in users). - Database 2: This houses the sales in the Orders table.
- Database 1:
Both tables will be linked on the
SalesRepID
column.- There are around around 352 users in the
ProPit_User
table. - There are a lot of orders in the orders table.
This code does what I need it to do; it is just dreadfully slow.
public static int GetNationalRank(int teamId, int CurrentUserId)
{
List<ProPit_User> lstProPit_User = new List<ProPit_User>();
List<TeamSales> lstTeamSales = new List<TeamSales>();
int currentUserRank = 0;
using (ProsPitEntities ppdb = new ProsPitEntities())
using (TraegerEntities db = new TraegerEntities())
{
lstProPit_User = ppdb.ProPit_User.Where(x => x.SalesRepID != null).ToList();
foreach (var user in lstProPit_User)
{
var strStartDate = DateTime.Now.AddDays(-(DateTime.Now.Day - 1)).ToString("M/d/yyyy");
var dtStartDate = Convert.ToDateTime(strStartDate);
var strEndDate = DateTime.Now.AddDays(1).ToString("M/d/yyyy");
var dtEndDate = Convert.ToDateTime(strEndDate);
var orders = db.Orders.Where(o => o.DateCompleted >= dtStartDate
&& o.DateCompleted < dtEndDate
&& (o.Status == 1 || o.Status == 2)
&& o.Kiosk != 0).AsEnumerable();
var total = (from o in orders
where o.SalesRepID == user.SalesRepID
select o.OrderTotal).Sum();
TeamSales TeamSales = new TeamSales();
TeamSales.userId = user.userID;
TeamSales.TotalSales = total;
lstTeamSales.Add(TeamSales);
}
}
int rank = lstTeamSales.OrderByDescending(x => x.TotalSales).FindIndex(x => x.userId == CurrentUserId);
if (rank == -1)
{
currentUserRank = 0;
}
else
{
// Zero based index need to add one to rank
currentUserRank = rank + 1;
}
}
return currentUserRank;
}
I have .FindIndex
added as an extension method to IEnumerable
.
All I really need is to get the rank of the person logged in in comparison to everyone else sales.