public static int GetNationalRank(int teamId, int CurrentUserId)
{
int currentUserRank;
var startDate = Convert.ToDateTime(DateTime.Now.AddDays(-(DateTime.Now.Day - 1)).ToString("M/d/yyyy"));
var endDate = Convert.ToDateTime(DateTime.Now.AddDays(1).ToString("M/d/yyyy"));
List<ProPit_User> proPitUsers;
using (var ppdb = new ProsPitEntities())
{
proPitUsers = ppdb.ProPit_User.Where(x => x.SalesRepID != null).ToList();
}
var teamSales = new List<TeamSales>(proPitUsers.Count());
using (var db = new TraegerEntities())
{
foreach teamSales.AddRange(varproPitUsers
.Select(user in=> proPitUsers)new
{
var orders user,
total = db.Orders.Where(o => o.DateCompleted >= startDate
&& o.DateCompleted < endDate
&& (o.Status == 1 || o.Status == 2)
&& o.Kiosk != 0).AsEnumerable();
var total = orders
.Where(o =>&& o.SalesRepID == user.SalesRepID)
.Select(o => (decimal?)o.OrderTotal)
.Sum() ?? 00.0M
.Sum(});
teamSales.AddSelect(userTotal => new TeamSales { userId = userTotal.user.userID, TotalSales = userTotal.total }));
}
var rank = teamSales
.OrderByDescending(x => x.TotalSales)
.FindIndex(x => x.userId == CurrentUserId);
currentUserRank = rank == -1 ? 0 : rank + 1;
}
return currentUserRank;
}