gpt4 book ai didi

c# - 使用 LINQ 分组和嵌套列表

转载 作者:太空宇宙 更新时间:2023-11-03 12:36:16 26 4
gpt4 key购买 nike

我有一个列表,其中包含以下夹具类的对象:

public class APIFixtureModel
{
public string HomeTeamName { get; set; }
public string AwayTeamName { get; set; }
public string TournamentName { get; set; }
public string SportType { get; set; }
public DateTime FixtureDateTime { get; set; }
}

因此,根据我当前的设置,我得到了一个固定装置列表,这很好,但我需要一个结构,其中列表按运动类型分组,然后按锦标赛分组。举例说明:

public class APIExtendedFixtureModel
{
private List<Sport> lstSports;

class Sport
{
public string SportType { get; set; }
public List<Tournament> lstTournaments;
}

class Tournament
{
public string TournamentName { get; set; }
public List<Fixture> lstFixtures;
}

class Fixture
{
public string HomeTeamName { get; set; }
public string AwayTeamName { get; set; }
public DateTime FixtureDateTime { get; set; }
}
}

我尝试了以下方法:

 var testgroup = lstFixtures.GroupBy(f => f.SportType,
f => f.TournamentName,
(key,g) => new
{
Sport = key,
Tournament = g.ToList()
}).ToList();

我得到的是一个运动列表,在每个运动节点中我都得到一个锦标赛列表,但它就此停止,我似乎无法正确处理。

最佳答案

这将返回填充有锦标赛和赛程的 Sport 对象列表:

List<Sport> sports = 
fixtureList.GroupBy(f => f.SportType).Select(sGroup => new Sport
{
SportType = sGroup.Key,
lstTournaments = sGroup.GroupBy(f => f.TournamentName).Select(tGroup => new Tournament
{
TournamentName = tGroup.Key,
lstFixtures = tGroup.Select(f => new Fixture
{
HomeTeamName = f.HomeTeamName,
AwayTeamName = f.AwayTeamName,
FixtureDateTime = f.FixtureDateTime,
}).ToList(),
}).ToList()
}).ToList();

关于c# - 使用 LINQ 分组和嵌套列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40886543/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com