Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 优雅高效的linq solultion,可计算剧集数_C#_Linq - Fatal编程技术网

C# 优雅高效的linq solultion,可计算剧集数

C# 优雅高效的linq solultion,可计算剧集数,c#,linq,C#,Linq,我有几门课是这样的: public class TvShow { public Title {get; set;} public List<Season> Seasons {get; set;} } public class Season { public int SeasonNumber {get; set;} public List<Episode> Episodes {get; set;} } public class Episo

我有几门课是这样的:

public class TvShow
{
    public Title {get; set;}
    public List<Season> Seasons {get; set;}
}

public class Season
{
    public int SeasonNumber {get; set;}
    public List<Episode> Episodes {get; set;}
}

public class Episode
{
    public int EpisodeNumber {get; set;}
}

我可以用linq做任何更优雅或更快的事情吗?

好吧,您的代码相当于:

int count = tvShows.SelectMany(show => show.Seasons)
                   .SelectMany(season => season.Episodes)
                   .Count();
或者,您可以使用
Sum
找出每个节目中有多少集,然后再次找出所有节目中的总集数:

int count = tvShows.Sum(show => show.Seasons.Sum(season => season.Episodes.Count));
int count = tvShows.SelectMany(show => show.Seasons)
                   .Sum(season => season.Episodes.Count);
或者是一种混合,只需将所有节目中所有季节的集数相加:

int count = tvShows.Sum(show => show.Seasons.Sum(season => season.Episodes.Count));
int count = tvShows.SelectMany(show => show.Seasons)
                   .Sum(season => season.Episodes.Count);

我希望最后一场是最有效率的——但事实上,除非你有太多的节目,否则我希望所有的选择都非常快。最后一个也是非常优雅的,所以这就是我想要的。

您的代码相当于:

int count = tvShows.SelectMany(show => show.Seasons)
                   .SelectMany(season => season.Episodes)
                   .Count();
或者,您可以使用
Sum
找出每个节目中有多少集,然后再次找出所有节目中的总集数:

int count = tvShows.Sum(show => show.Seasons.Sum(season => season.Episodes.Count));
int count = tvShows.SelectMany(show => show.Seasons)
                   .Sum(season => season.Episodes.Count);
或者是一种混合,只需将所有节目中所有季节的集数相加:

int count = tvShows.Sum(show => show.Seasons.Sum(season => season.Episodes.Count));
int count = tvShows.SelectMany(show => show.Seasons)
                   .Sum(season => season.Episodes.Count);
我希望最后一场是最有效率的——但事实上,除非你有太多的节目,否则我希望所有的选择都非常快。最后一个也很优雅,所以这就是我想要的

tvShows.SelectMany(show => show.Seasons)
       .Sum(season => season.Episodes.Count)
差不多

tvShows.SelectMany(show => show.Seasons)
       .Sum(season => season.Episodes.Count)

它是否比foreach senario更高效?你能直接从一个节目转到另一个这样的剧集吗?@HosseinNarimaniRad:我补充了一点。“我怀疑这有什么关系。”琼斯凯啊,和我们其他人一样人性!我在看到编辑后删除了我的评论。没有人会知道……;)只是出于兴趣,你为什么期望一个组合最快?它比foreach senario更有效吗?你能直接从节目转到这样的剧集吗?@HosseinNarimaniRad:我补充了一点。“我怀疑这有什么关系。”琼斯凯啊,和我们其他人一样人性!我在看到编辑后删除了我的评论。没有人会知道……;)只是出于兴趣,你为什么希望混音最快?你知道这样暴露列表会让你陷入困境吗?@TonyHopkinson怎么会这样?任何使用该实例的人都可以将列表更改为新的列表。这可能不是问题,但是一些git会怎么做myTvShow.Seasons=nil?你知道吗?你知道这样暴露列表会让你陷入一个大洞吗?@TonyHopkinson怎么会这样?任何使用该实例的人都可以将列表更改为新列表。这可能不是问题,但是一些git会怎么做myTvShow.Seasons=nil?你觉得怎么样?