Reputation:
I have following csv data
2,1,3,5,0 3,1,3,20,0 4,1,6,200,1000
I am having trouble calculating the percentage efficiently. Here is what I have so far:
var csvParser = new CsvParser();
var customers = csvParser.ParseCsv(@"data.csv");
var results = from c in customers
let total = customers.Sum(x => x.Stake)
let totalWin = customers.Sum(x => x.Win)
group c by new
{
Id=c.Id,
Stake=c.Stake,
Win=c.Win,
} into history
select new
{
Id = history.Key.id,
percentage = (float)history.Count() / (float)e.Key.total
};
return results;
Upvotes: 0
Views: 1848
Reputation: 20504
You're way over complicating it. I'll assume you mixed that stake stuff because this was part of another query, also the let's are completely unnecessary.
You just have to group by the Id and then divide the group.Count(of Win) by the group.Count() to get the win percent.
var cheaters = customers.GroupBy(c => c.Id)
.Select(g => new
{
Id= g.Key,
Percentage = g.Count(c => c.Win > 0) / (double)g.Count()
// feel free to aggreate other values here like g.Sum(x => x.Stake)
})
.Where(c => c.Percentage >= .6);
Upvotes: 3