2016-11-23 34 views
1

我有TypeScript代码,我需要在C#中的等效代码。在C#中的TypeScript“地图”功能?

声明:

private sessionCommands: SessionCommand[]; 
// . . . 
// Create array in constructor. 
this.sessionCommands = new Array(); 
// . . . 
// Push few objects to array in some method 

,然后得到的数据。这是重要的一部分,如何在C#中做到这一点?

var data = this.sessionCommands.map(x => x.identifier + " " + x.getParameter() + ";").join("\n"); 
+4

这是LINQ的选择方法。 – Evk

+1

而不是显示TypeScript代码,描述你想要实现的内容会更有用。这样,你不依赖于同时知道TypeScript *和* C#的读者。我的猜测是,你只是从LINQ寻找'Select'方法... –

+0

我正在从类型脚本进行迁移。我不是100%确定我在做什么:) – Raskolnikov

回答

4

在.NET世界相对应的是选择功能:

public static IEnumerable<TResult> Select<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> selector); 

它适用于各种枚举类型(包括阵列)。但是这是一种扩展方法,您必须导入System.Linq才能使用它。为您的代码

完整的示例:

var data = String.Join("\n", this.sessionCommands.Select(x => x.identifier + " " + x.getParameter() + ";"));