I'm have an issue with high performance inside a loop. I'm not sure how I can optimise the code to be honest. With the Visual Studio Performance Explorer I've managed to fix a few bugs I've had, but now the problem seems to be due to the number of loops this particular for loop is doing.
Here's a screenshot from Visual Studio:
I'm guessing that since the int i = 0
is being marked the performance issue is because of a large number of loops. Alsp, I know that ExecuteReader
is marked as high performance but I don't think I can change that, I mean it's the requests I have from my database.
EDIT
Code:
public IQueryable<Match> GetMatches()
{
DLTxOdds dlTxOdds = new DLTxOdds();
IQueryable<Match> matches = dlTxOdds.GetMatches();
List<Match> mList = new List<Match>();
// For each retrieved match check whether it contains any sure bets
foreach (Match m in matches)
{
List<SureBet> matchSureBets = new List<SureBet>();
// Get sport of match
m.sport = dlTxOdds.GetMatchSport(m.sportid);
// Get orders of match, ordered by the odd type id
m.matchOffers = dlTxOdds.GetMatchOffers(m.matchid).OrderBy(o => o.ot).ToList();
int tempID = -1;
List<Offer> tempMatchOffersList = new List<Offer>();
// For every match offer get its respective bookmaker, odds and odd type
for (int i = 0; i < m.matchOffers.Count; i++)
{
m.matchOffers[i].bookmaker = dlTxOdds.GetBookmaker(m.matchOffers[i].bid);
m.matchOffers[i].odds = dlTxOdds.GetOfferOdds(m.matchOffers[i].offer_id);
m.matchOffers[i].oddType = dlTxOdds.GetOddType(m.matchOffers[i].ot);
// Filter the match offers so that offers with the same odd type id are checked for sure bets
tempID = m.matchOffers[i].ot;
// if current index is last one we cannot check for the next element therefore add to list and check for sure bets
if (i + 1 != m.matchOffers.Count)
{
// if next odd type in list is different, check for sure bets
if (m.matchOffers[i + 1].ot != tempID)
{
tempMatchOffersList.Add(m.matchOffers[i]);
// Get sure bets
matchSureBets.AddRange(GetSureBets(tempMatchOffersList));
tempMatchOffersList = new List<Offer>();
}
// else add to temporary list
else
tempMatchOffersList.Add(m.matchOffers[i]);
}
else
{
tempMatchOffersList.Add(m.matchOffers[i]);
// Get sure bets
matchSureBets.AddRange(GetSureBets(tempMatchOffersList));
}
}
if (m.matchOffers.Count() != 0)
{
// if match has any sure bets add to list...
if (matchSureBets.Count() != 0)
{
m.sureBets = matchSureBets.Where(s => s.max == true).OrderByDescending(s => s.profit).ToList();
mList.Add(m);
}
}
}
mList = mList.OrderByDescending(m => m.sureBets[0].profit).ToList();
return mList.AsQueryable<Match>();
}