我有以下类:
public partial class MemberTank
{
public int Id { get; set; }
public string TankName { get; set; }
public int Battles { get; set; }
public int Victories { get; set; }
}
我正试图构建一个LINQ,为我提供一个TankName列表,其中包含每辆坦克的胜利百分比。我知道我需要把战斗和胜利的总和分开,但我不知道该怎么做。
求一个或另一个的和很容易。。。
var sumVictories = (from p in db.MemberTanks
group p by p.TankName into g
select new {
Tank = g.Key,
Victories = g.Sum(p => p.Victories)
}).OrderByDescending(a => a.Victories);
但我不知道如何将两个字段相加,然后将它们除以,得到我需要的百分比。
这应该有效。。。
var sumVictories = (from p in memberTanks
group p by p.TankName
into g
select new
{
Tank = g.Key,
Victories = g.Sum(p => p.Victories),
Avg = ((double)g.Sum(p => p.Victories) / (double)g.Sum(p => p.Battles)) * 100
}).OrderByDescending(a => a.Victories);
[edit]清理了代码一点