gpt4 book ai didi

c# - Linq 过滤器集合内部集合

转载 作者:太空宇宙 更新时间:2023-11-03 20:53:58 25 4
gpt4 key购买 nike

'我有一个包含这些细节的集合

Class Team
{
string TeamName;
Int TeamId;
List<matches>Match
}

class Matches
{
String MatchName;
Int Season;
}

我有大约 16 个团队,我需要选择一个特定团队的数据大于 1998 年。哪种方法最好?我尝试使用 .Any 但它会跳过并且只采用季节过滤条件。有帮助吗?

最佳答案

据我了解,您需要所有团队的列表,其中包含匹配条件的匹配项,但包含基于此条件的匹配过滤器列表。具有以下数据:

var allteams = new List<Team> {
new Team
{
TeamName = "FooBar SO",
TeamId = 1,
Matchs = new List<Match>
{
new Match{ MatchName="ok" , Season=100 },
new Match{ MatchName="notOk" , Season=10 }
}
},
new Team
{
TeamName = "Other SO",
TeamId = 2,
Matchs = new List<Match>
{
new Match{ MatchName="nope" , Season=20 },
new Match{ MatchName="notOk" , Season=10 }
}
}
};

1/。至少有一场比赛符合条件的所有球队。
球队比赛名单保留。

var teamsFilterOnSeason 
= allteams.Where(t => t.Matchs.Any(m => m.Season >= 100));

结果:

 TeamName: FooBar SO,
TeamId: 1,
Matchs:
[
{ MatchName: ok, Season: 100 },
{ MatchName: notOk, Season: 10 }
]

2/。至少有一场比赛符合条件的所有球队。
匹配列表是根据条件过滤的。

var teamsWithSeasonFilter 
= allteams
.Where(t => t.Matchs.Any(m => m.Season >= 100))
.Select(
t =>
new Team {
TeamName = t.TeamName,
TeamId = t.TeamId,
Matchs= t.Matchs
.Where(m=> m.Season >= 100)
.ToList()
}
);

结果:

TeamName: FooBar SO,
TeamId: 1,
Matchs:
[
{ MatchName: ok, Season: 100 }
]

关于c# - Linq 过滤器集合内部集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52424485/

25 4 0
文章推荐: c# - 带有 Saxon API 的 XQuery/XPath - 需要不区分大小写的路径
文章推荐: css - HTML
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com