`

C# - Select many as the flatMap in Scala

    博客分类:
  • C#
c# 
阅读更多

If you are faimiar with Scala, you must have heard or used the function flatMap which is a very useful function that enable you to flat a collection. 

 

There is no such flatMap or Map method as in Scala, however, the C# linq has provide you with the necessary tools to allow you to do the flat map.

 

this has been discussed in this post "USE LINQ'S SELECTMANY METHOD TO "FLATTEN" COLLECTIONS", it has been discussed. 

 

Below I will save the droll and let's see the code. 

 

 

class League
  {
    public string Name { get; set; }
    public Team[] Teams { get; set; }
  }

  class Team
  {
    public string Name { get; set; } 
    public Player[] Players { get; set; }
    public int NumberOfWins { get; set; }
    public string HomeState { get; set; } 
  }

  class Player 
  {
    public string Name { get; set; }
    public int Age { get; set; } 
  }

  class Program
  {
    static void Main(string[] args)
    {
      var leagues = new List<League>
      {
        new League {
          Name = "AFC-West",
          Teams = new []{
            new Team {
              Name = "Chargers",
              NumberOfWins = 3,
              HomeState = "CA",
              Players = new [] {
                new Player { Name = "Rivers", Age = 30 },
                new Player { Name = "Tomlinson" , Age = 23},
                new Player { Name = "Gates" , Age = 18},
              }
            },
            new Team {
              Name = "Croncos",
              NumberOfWins = 2,
              HomeState = "LOS",
              Players = new [] {
                new Player { Name = "Culter", Age = 23 },
                new Player { Name = "Bailey", Age = 28 },
                new Player { Name = "Marshal", Age = 30 },
              }
            },
           }
         },
         new League {
           Name = "AFC-South",
           Teams = new [] {
            new Team {
              NumberOfWins = 1,
              Name = "Colts",
              HomeState = "NY",
              Players = new [] {
                new Player { Name = "Manning", Age = 31 },
                new Player { Name = "Bailey" , Age = 19},
                new Player { Name = "Vinatieri" , Age = 26},
              }
            },
           }
         },
        
      };


      var allTeams = from t in leagues.
                     SelectMany(l => l.Teams)
                     select t;

      var allPlayers = from p in leagues
                       .SelectMany(l => l.Teams)
                       .SelectMany(t => t.Players)
                       select p;

      var onlyYoungPlayers = from p in leagues
                             .SelectMany(l => l.Teams)
                             .SelectMany(t => t.Players)
                             where p.Age < 25
                             select p;

      var players = leagues.SelectMany(l => l.Teams)
                     .Where(t => t.NumberOfWins > 2)
                     .Where(p => p.HomeState == "CA")
                     .Select(p => p);

      var teamsAndTheirLeagues = from helper in leagues
                                 .SelectMany(l => l.Teams, (league, team) => new { league, team })
                                 where helper.team.Players.Length > 2
                                   && helper.league.Teams.Length < 10
                                   select new { LeagueID = helper.league.Name, Team = helper.team };
    }
  }
分享到:
评论

相关推荐

    scala-2.12.14.zip&scala-2.12.11.tgz Linux版本压缩包.rar

    标题提到的"scala-2.12.14.zip&scala-2.12.11.tgz"是Scala的不同版本,针对不同的操作系统进行了打包。Windows用户可以使用".zip"格式的文件,而Linux用户则适合使用".tgz"格式的文件。这里我们主要关注Linux版本的...

    scala-parser-combinators_2.11-1.0.4-API文档-中英对照版.zip

    赠送jar包:scala-parser-combinators_2.11-1.0.4.jar; 赠送原API文档:scala-parser-combinators_2.11-1.0.4-javadoc.jar; 赠送源代码:scala-parser-combinators_2.11-1.0.4-sources.jar; 包含翻译后的API...

    Algorithm-algorithm-implementation-in-java-javascript-scala.zip

    Algorithm-algorithm-implementation-in-java-javascript-scala.zip,本项目的目标是提供Java、JavaScript和斯卡拉的可重用算法集合。,算法是为计算机程序高效、彻底地完成任务而创建的一组详细的准则。

    scala-compiler-2.11.8-API文档-中英对照版.zip

    赠送jar包:scala-compiler-2.11.8.jar; 赠送原API文档:scala-compiler-2.11.8-javadoc.jar; 赠送源代码:scala-compiler-2.11.8-sources.jar; 赠送Maven依赖信息文件:scala-compiler-2.11.8.pom; 包含翻译后...

    scala-maven-plugin-4.4.0.jar

    maven的scala编译包

    Scala-2.11.1.zip

    在Scala-2.11.1.zip这个压缩包中,我们看到的是Scala的特定版本2.11.1。这个版本是在Scala 2.x系列中的一个稳定版,提供了许多改进和新特性。Scala 2.11.x系列的一个主要目标是提升与Java 7和8的兼容性,同时引入对...

    scala-maven-plugin:scala-maven-plugin(以前是maven-scala-plugin)用于在maven中编译testingrunningdocumenting scala代码

    Scala Maven 插件 scala-maven-plugin (以前是 maven-scala-plugin)用于在 Maven 中编译/测试/运行/记录 Scala 代码。类似插件 建造目前,您需要 Maven 3.x 和 JDK 8 来构建插件、创建站点并运行integration-test ...

    scala-compiler.jar

    scala-compiler.jar

    scala-intellij-bin-2016.3.9

    "scala-intellij-bin-2016.3.9"是针对Scala语言的一个特定版本的IntelliJ IDEA插件,该版本为2016.3.9。这个插件是专门为Scala开发者设计的,旨在提高他们在IntelliJ IDEA中的开发体验。 Scala是一种多范式编程语言...

    rock-the-jvm-scala-beginners:Scala基础

    "Rock the JVM Scala Beginners" 提供了一个适合初学者的学习资源,帮助开发者掌握Scala的基础知识。在这个压缩包中,你可能会找到一系列的练习、示例代码和教程,让你逐步深入Scala的世界。 首先,Scala运行在Java...

    mongo-scala-drive的使用demo

    在 Scala 中与 MongoDB 进行交互,通常我们会使用 `mongo-scala-driver`,而不是 `mongo-java-driver`,因为 Scala 驱动提供了更符合 Scala 语言特性的 API 设计。本示例将详细介绍如何使用 `mongo-scala-driver` ...

    flink-1.19.0-bin-scala-2.12.tgz flink-1.16.3-bin-scala-2.12.tgz

    标题和描述中提到的"flink-1.19.0-bin-scala-2.12.tgz"和"flink-1.16.3-bin-scala-2.12.tgz"是Apache Flink的两个不同版本的二进制发行版。Flink是一个开源的流处理和批处理框架,它提供了低延迟、高吞吐量的数据...

    scala-actors-2.10 jar包

    scala-actors-2.10 jar包,可以解决 scala-actors-2.10以上版本带来的不兼容问题

    scalajs-vue, Vue.js的Scala.js 绑定和示例应用程序.zip

    scalajs-vue, Vue.js的Scala.js 绑定和示例应用程序 scalajs-vue用于 Vue.js的Scala.js-绑定和示例应用程序要作为图书馆使用,只要从https://jitpack.io/#fancellu/scalajs-vue 示例应用展示了使用 Scala.j

    scala-js-ts-importer, Scala.js的打印机导入程序.zip

    scala-js-ts-importer, Scala.js的打印机导入程序 从打字类型定义到 Scala.js的导入程序这里工具读取为 TypeScript 编写的类型定义文件,并将它们重写为可以使用 Scala.js的. scala 文件。过程不精确 100 %,因此...

    flink-1.18.1-bin-scala-2.12 免费下载

    flink-1.18.1-bin-scala-2.12 免费下载flink-1.18.1-bin-scala-2.12 免费下载flink-1.18.1-bin-scala-2.12 免费下载flink-1.18.1-bin-scala-2.12 免费下载flink-1.18.1-bin-scala-2.12 免费下载flink-1.18.1-bin-...

    flink-1.9.1-bin-scala_2.11.tgz

    flink-1.9.1-bin-scala_2.11.tgz flink-1.9.1-bin-scala_2.11.tgz flink-1.9.1-bin-scala_2.11.tgz flink-1.9.1-bin-scala_2.11.tgz

    scala-parser-combinators-2.11-1.0.4-API文档-中文版.zip

    赠送jar包:scala-parser-combinators_2.11-1.0.4.jar; 赠送原API文档:scala-parser-combinators_2.11-1.0.4-javadoc.jar; 赠送源代码:scala-parser-combinators_2.11-1.0.4-sources.jar; 赠送Maven依赖信息...

    flink-1.12.7-bin-scala_2.12.tgz

    这个压缩包 "flink-1.12.7-bin-scala_2.12.tgz" 包含了 Flink 1.12.7 的二进制版本,特别针对使用 Scala 2.12 的开发环境进行了优化。 1. **Flink 的核心特性** - **流处理**:Flink 提供了一种无界限数据的概念,...

    scala-intellij-bin-2023.1.15.zip

    "scala-intellij-bin-2023.1.15.zip" 是一个包含IntelliJ IDEA针对Scala开发的特定版本的二进制发行包。 这个压缩包很可能包含了以下内容: 1. **IntelliJ IDEA安装程序**:这是主应用程序,允许用户在本地计算机...

Global site tag (gtag.js) - Google Analytics