Creating a list by matching/combining items from two separate list
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 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.
RichPosted Apr 25, 2012, 3:59 PM
VulpesPosted Apr 25, 2012, 3:52 PM
RichPosted Apr 25, 2012, 2:56 PM
The Except extension method was the main thing that helped. My question now is, is there a more efficient or
concise way to write the above code?
VulpesPosted Apr 25, 2012, 4:15 AM
RichPosted Apr 24, 2012, 9:32 PM
Teams: AwayRecord: HomeRecord:
t1 t1 t1
t2 t2 t2
t3 none t3
t4 t4 none
t5 t5 t5
to
mlbStandings:
t1 has AwayRecord and HomeRecord properties
t2 has AwayRecord and HomeRecord properties
t3 has null and HomeRecord properties
t4 has AwayRecord and null properties
t5 has AwayRecord and HomeRecord properties
Sorry for any confusion.
VulpesPosted Apr 24, 2012, 3:51 PM