2016-12-15 5 views
0

ich nicht herausfinden können, wie in c# eine Liste der Zeichenfolge auf der Karte,Array.prototype.Map - C# IEnumerable Equivalent

kann ich etwas Ähnliches diese js:

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

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

aber mit IEnumerable<string> in C#:

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

var posts = ?? 
+7

Mit LINQ: 'var posten = ids.Wählen (s =>" post- "+ s)' – haim770

+0

Was sollte das Ergebnis sein, was ist 'Beiträge'? Ist das ein IDictionary oder IEnumerable? – Johnny

Antwort

2
int[] items = new int[] { 12, 23, 14, 15, 65, 66, 33 }; 

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

IEnumerable.Select() ist Ihr map() gleichwertig.

+0

ist die Syntax '$" post- {x} "' in allen C# Versionen verfügbar? – Hitmands

+1

@Hitmands, nein, [String-Interpolation] (https://msdn.microsoft.com/en-us/library/dn961160.aspx) wurde in C# 6 eingeführt. – kiziu