【问题标题】:Array.prototype.Map - C# IEnumerable EquivalentArray.prototype.Map - C# IEnumerable 等效项
【发布时间】:2017-05-01 01:09:31
【问题描述】:

我不知道如何在c# 中映射字符串列表,

我能有类似这个js的东西吗:

var items = [12, 23, 14, 15, 65, 66, 33];

var ids = items.map(id => `post-${id}`);

但在C# 中使用IEnumerable<string>

IEnumerable<string> ids = Product.GetRelatedProductsIds();

var posts = ?? 

【问题讨论】:

  • 使用 LINQ:var posts = ids.Select(s =&gt; "post-" + s)
  • 应该是什么结果,posts是什么?那是 IDictionary 还是 IEnumerable?

标签: c# linq functional-programming ienumerable


【解决方案1】:
int[] items = new int[] { 12, 23, 14, 15, 65, 66, 33 };

IEnumerable<string> ids = items.Select(x => $"post-{x}");

IEnumerable.Select() 是您的 map() 等效项。

【讨论】:

  • 语法 $"post-{x}" 在所有 C# 版本中都可用吗?
  • @Hitmands,不,string interpolation 是在 C# 6 中引入的。
猜你喜欢
  • 2011-01-29
  • 2010-09-17
  • 1970-01-01
  • 1970-01-01
  • 2010-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多