I am trying to do a simple calculation and for some reason my return variable is zero. Within my model, I am trying to get a Total of all yes votes and then divide that by the total number of votes. Here are a few relevant classes.
private int _total = -1;
public int Total
{
get
{
if (_total < 0)
{
_total = get_total(TopicId);
}
return _total;
}
}
private int get_total(int id)
{
int itotal = 0;
int ycount = 0;
ApplicationDbContext db = new ApplicationDbContext();
List<Vote> VoteList = db.Votes.Where(t => t.TopicId == id).ToList();
if (VoteList != null && VoteList.Count > 0)
{
//lcount = VoteList.Count();
foreach (Vote sub in VoteList)
{
var c = from v in VoteList
where v.Score == true
select v.VoteId;
ycount = c.Count();
itotal = ycount / VoteList.Count();
}
}
return itotal;
}
In the for each, if I debug "ycount" is equal to the correct number and Votelist.count is equal to the right number, but itotal is 0. I also attempted to make votelist.count = to a variable, but that produced the same results. I am pretty new to c#, so I'm hoping this is obvious, but what am I missing?