我有两个Ienumebles。首先是排球,篮球,足球比赛。 第二 - 完整的游戏历史。它的所有字符串,因为我解析它的
public class Events
{
public string Date { get; set; }
public string FirstTeam { get; set; }
public string SecondTeam { get; set; }
}
public class History
{
public string Date { get; set; }
public string FirstTeam { get; set; }
public string FirstTeamGoals { get; set; }
public string SecondTeam { get; set; }
public string SecondteamGoals { get; set; }
}
我需要展示之前参加比赛的球队比赛。在之前的比赛中,球队可以成为第一或第二阵容。
我试试这个:
foreach (var teamInEvent in ListEvents)
{
var firstor = from p in History
where p.FirstTeam == teamInEvent.FirstTeam || p.SecondTeam == teamInEvent.FirstTeam
where p.SecondTeam == teamInEvent.SecondTeam || p.FirstTeam == teamInEvent.SecondTeam
select p;
}
因此我需要展示Date,FirstTeam,FirstTeamGoals,SecondTeam,SectGoals。比较目标和节目:团队赢了最后3场比赛(例如)。
答案 0 :(得分:1)
如果我理解了问题"第一个团队"和#34;第二支球队"在语义上没有区别。如果是这种情况,您需要包含第一个团队列为"第二个"反之亦然。
IQueriable<History> GetHistory(Events teamInEvent)
{
// Normal Query
var firstQuery =
from h1 in History
select h1
where h1.FirstTeam == teamInEvent.FirstTeam ||
h1.SecondTeam == teamInEvent.SecondTeam;
// Query with the first and the second team fields swapped
var secondQuery =
from h2 in History
select new History { Date = h2.Date,
FirstTeam = h2.SecondTeam,
FirstTeamGoals = h2.SecondTeamGoals,
SecondTeam = h2.FirstTeam,
SecondTeamGoals = h2.FirstTeamGoals
}
where h2.FirstTeam == teamInEvent.SecondTeam ||
h2.SecondTeam == teamInEvent.FirstTeam;
// Stitch two queries together
return firstQuery.Concat(secondQuery);
}
上述函数返回Events
要整合所有条目,只需迭代将结果添加到列表中的事件:
var list = new List<Tuple<Events, History[]>;
foreach (var teamInEvent in ListEvents)
{
var item = Tuple.Create(teamInEvent, GetHistory(teamInEvent).ToArray());
list.Add(item);
}
您不仅可以存储历史数据,还可以处理历史记录,计算您需要的统计数据。
foreach (var teamInEvent in ListEvents)
{
var history = GetHistory(teamInEvent);
var stats = ComputeStats(teamInEvent, history);
list.Add(Tuple.Create(teamEvent, stats));
}