Rich

Rich

  • NA
  • 84
  • 61.3k

Creating a list by matching/combining items from two separate list

Apr 24 2012 2:46 PM
The statement below works fine when both the AwayRecord and HomeRecord objects returns every team associated with the Teams object, but there are times when this will not be the case.  For example, the Teams object will always return 30 items, while the AwayRecord may return 28, and the HomeRecord 29.  I would like to specifically match the AwayRecord and HomeRecord items to the matching team items from the Teams object.  Where there are no matching items from either the AwayRecord or HomeRecord with the Teams object, I would like to retain the team name from the the Teams object and just set the remaining properties to null or 0.

List<MLBrst> mlbStandings = Teams.Select((t, i) => new MLBrst
(t.Abreviated_Short, AwayRecord[i], HomeRecord[i])).AsEnumerable().ToList();

// MLBrst constructor
public MLBrst(string team, MLBrst ar, MLBrst hr)
{
 Team = team;
 Wins = ar.Wins + hr.Wins;
 Losses = ar.Losses + hr.Losses;
 Spread = (ar.Spread + hr.Spread) / (Wins + Losses);
 Total = (ar.Total + hr.Total) / (Wins + Losses);
 GP = Wins + Losses;
 RS = ar.RS + hr.RS;
 RA = ar.RA + hr.RA;
 PD = RS - RA;
}

Thanks in advance.

Answers (6)