I have a method that contains nested parallel loops to populate one of the properties of the loop.
public static string CalculateFantasyPointsLeagueSettings(ref List<Projections> projection, League league, Teams teamList)
{
string PPR = "";
var stats = (from a in league.Settings.StatCategories.stats
join b in league.Settings.StatModifiers.stats on a.StatId equals b.StatId
select new Tuple<string, double>(a.Name.Replace(" ", string.Empty), b.Value)).ToList();
var props = new Projections().GetType().GetProperties();
double receptionValue = (stats.Where(a => a.Item1 == "Receptions").Select(a => a.Item2).FirstOrDefault());
if (receptionValue == 1.0)
{
PPR = "PPR";
}
else if (receptionValue == .5)
{
PPR = "Half PPR";
}
Parallel.ForEach(projection, proj =>
{
double points = 0;
Parallel.ForEach(props, prop =>
{
var stat = (from a in stats
where a.Item1 == prop.Name
select
Convert.ToDouble(prop.GetValue(proj, null)) * a.Item2
).FirstOrDefault();
points += stat;
});
proj.ProjectedPoints = Math.Round(points, 2);
proj.FantasyTeam = (from a in teamList.TeamList
where a.Players.player.Select(b => b.Name.Full).Contains(proj.Name)
select a.Name).FirstOrDefault();
});
return PPR;
}
What this method does is calculates the points for a fantasy football player based on their league settings (stats object) and their projected stats (projections list). Since stats is not a static list (you can have custom settings), I need to be able to loop through all the items in that list, and calculate each one by hand. In order to do this, I have to get the property in Reflection and than use it's value. Since projection has about 35k records and stats will be well over 30, this method takes a long time to run, probably due to reflection. I am trying to figure out if there is anything I can do to make it run faster. Right now it takes about 2-3 seconds, which is not ideal. I cannot put it in cache as it is fairly dynamic. Any help would be greatly appreciated.