C# 中的 TypeScript“地图”函数?

Ras*_*kov 2 c# linq typescript

我有 TypeScript 代码,并且需要 C# 中的等效代码。

宣言:

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

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

var data = this.sessionCommands.map(x => x.identifier + " " + x.getParameter() + ";").join("\n");
Run Code Online (Sandbox Code Playgroud)

Mat*_*247 5

.NET 世界中的等效函数是 Select 函数:

public static IEnumerable<TResult> Select<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> selector);
Run Code Online (Sandbox Code Playgroud)

它适用于各种可枚举类型(包括数组)。然而它是一个扩展方法,你必须导入System.Linq才能使用它。

您的代码的完整示例:

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