C# 比较两个IEnumerable
我有两个Ienumerables。首先是排球、篮球、足球项目。 第二,游戏的完整历史。它都是字符串,因为我解析它的C# 比较两个IEnumerable,c#,asp.net-mvc,linq,ienumerable,C#,Asp.net Mvc,Linq,Ienumerable,我有两个Ienumerables。首先是排球、篮球、足球项目。 第二,游戏的完整历史。它都是字符串,因为我解析它的 public class Events { public string Date { get; set; } public string FirstTeam { get; set; } public string SecondTeam { get; set; } } public class History
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;
}
因此,我需要显示日期、FirstTeam、FirstTeam目标、SecondTeam目标。比较进球并展示:球队在过去3场比赛中获胜(例如)。如果我理解这个问题,“一线队”和“二线队”在语义上没有区别。如果是这种情况,您需要包括第一个团队被列为“第二个”的记录,反之亦然
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);
}
您可以通过计算所需的统计数据来处理历史,而不仅仅是存储历史数据
foreach (var teamInEvent in ListEvents)
{
var history = GetHistory(teamInEvent);
var stats = ComputeStats(teamInEvent, history);
list.Add(Tuple.Create(teamEvent, stats));
}
您的
Date
属性的类型应为DateTime
,您的FirstTeamGoals
和SecondteamGoals
的类型应为int
(您将无法使用字符串进行任何比较)
foreach (var teamInEvent in ListEvents)
{
var history = GetHistory(teamInEvent);
var stats = ComputeStats(teamInEvent, history);
list.Add(Tuple.Create(teamEvent, stats));
}