I have a sql table called predictions with data as below
Week Player Points
201101 Mark 7
201101 Mark 7
201101 Pete 7
201101 Pet
It was somewhat confusing to see that your query didn't seem to correspond to the data. So instead, this will be based on the data alone. The query should produce valid SQL so you won't have to use LINQ to Objects. You can adapt it to your tables with little modification.
var query = from pred in Predictions
group pred.Points by pred.WeekNum into week
join pred in Predictions
on new { WeekNum = week.Key, Points = week.Max() }
equals new { pred.WeekNum, pred.Points }
group 1 by pred.Player into player
let Wins = player.Count()
orderby Wins descending, player.Key
select new
{
Player = player.Key,
Wins,
};
Just pass a datatable to following function (Please note that the code is in c#) //dt should contain column points but not rank column
public static DataTable GetRankedDatatable(DataTable dt)
{
var rankedDt = (from row in dt.AsEnumerable()
orderby row.Field<string>("points")
select row).CopyToDataTable();
rankedDt.Columns.Add("rank");
int rank = 0;
for (int i = 0; i < rankedDt.Rows.Count - 1; i++)
{
rankedDt.Rows[i]["rank"] = rank;
if (rankedDt.Rows[i]["points"].ToString() != rankedDt.Rows[i + 1]["points"].ToString())
{
rank++;
}
}
rankedDt.Rows[rankedDt.Rows.Count - 1]["rank"] = rank;
return rankedDt;
}
This code seems to be what you need:
var result = this.DataList
.GroupBy(data => data.Week)
.Select(data=>
{
return data.GroupBy(item => item.Name)
.Select(item => new { Name = item.Key, SumPoints = item.Sum(v => v.Points) })
.OrderBy(item => item.SumPoints)
.FirstOrDefault();
})
.GroupBy(_=>_.Name)
.ToDictionary(_=>_.Key, _=>_.Count());
mine is quite long
var weeklyTopScore = from eachMatch in FootballPredictions
group eachMatch by eachMatch.week
into weekly
select new {week = weekly.Key, topScore = weekly.Max(match => match.points)};
var playerWins = from eachResult in weeklyTopScore
join entry in FootballPredictions
on eachResult.week equals entry.week
where eachResult.topScore == entry.points
group entry by entry.player
into winner
select new { player = winner.Key, wins = winner.Count() };
var result = from entry in playerWins
group entry by entry.wins
into summary
select new { player = summary.Select(data => data.player).Aggregate((cur, nex) => cur + ", " + nex), wins = summary.Key};
Try:
p.Points.Any()
Instead of:
p.Points != null