I have a list a simple Player object, as follows
Name | Team | Position | Total Tom Brady | Team 1 | QB | 200 Adrian Peterson | Team 1 | RB | 250 Calvin Johnson | Team 2 | WR | 260 LeVon Bell | Team 2 | RB | 220 Peyton Manning | Team 3 | QB | 220 Arian Foster | Team 3 | RB | 220
This is a simple sample, in reality there are about 200 records. What I want to do is to get all possible combinations of players per team, and sum their total, so the end product would be as follows
Possibilities
Teams | Players | Total Team 1 | Tom Brady, Adrian Peterson | 450 Team 2 | Calvin Johnson, LeVon Bell | 480 Team 3 | Peyton Manning, Arian Foster | 440
Basically I am looking for trade possibilities, so I need to get combinations of players per team. The largest possible combination I am looking for is 5 players per team, where I would have the Players and their points combined in a new object. Right now I can get there with below.
var playerList = players.GroupBy(p => p.Team)
.Select(g => new
{
Team = g.Key,
g
}).ToList();
List<Possibilities> possibilities = new List<Possibilities>();
foreach (var a in playerList)
{
List<Possibilities> onePlayer = (from b in a.g
select new Possibilities
{
Players = b.Name,
Total = b.Total,
Team = a.Team
}).ToList();
List<Possibilities> twoPlayer = (from b in a.g
from c in a.g
select new Possibilities
{
Players = b.Name + ", " + c.Name,
Total = b.Total + c.Total,
Team = a.Team
}).ToList();
And this gives me all combinations of 1,2,3 players per team, but I want to add 4 and 5. This also does not remove duplicate combinations (Player 1, Player 2 and Player 2,Player1). Is there a cleaner way to do this?