Im running a photocontest and making a list of winners with one winner each day. My loop is running very slow. It seams to be the toList() that is time consuming. Any suggestions on how to make it run faster?
foreach (var date in dates)
{
var voteList = _images.Select(image => new ImageWinnerVotes
{
ID = image.ID,
Image = image.ImagePath,
Thumbnail = image.ThumbnailImagePath,
Title = image.Name,
Url = GetFriendlyUrl(image.ID),
Date = image.CreatedDate,
WinnerDateString = date.Date.ToString("dd/MM"),
WinnerDate = date.Date,
TotalVotes =
(votes.Where(vote => vote.ItemId == image.ID)).Count(),
Name = image.Fields["Name"].ToString(),
Votes =
(votes.Where(
vote =>
vote.ItemId == image.ID &&
vote.Date.DayOfYear == date.Date.DayOfYear)).Count()
}).ToList();
voteList = voteList.OrderByDescending(v => v.Votes).ToList();
var j = 0;
var findingWinner = true;
while(findingWinner)
{
var inWinnersList = false;
if (winnersList.Count() != 0)
{
if(!winnersList.Contains(voteList.ElementAt(j)))
{
winnersList.Add(voteList.ElementAt(j));
findingWinner = false;
}
}
else
{
winnersList.Add(voteList.ElementAt(j));
findingWinner = false;
}
j++;
}
}
GetFriendlyUrl(image.ID)
do ? \$\endgroup\$votes
and a list ofimage.ID
s to assemble your winners list long before constructing theImageWinnerVotes
, once you have thatwinnersList
ofimage.ID
then construct theImageWinnerVotes
from that so you don't have to create any extras that end up throw away. \$\endgroup\$