Example of array.map() in C#?

Consider the following common JavaScript construct

var ages = people.map(person => person.age);

Giving the desired result, which is an array of ages.

What is the equivalent of this in C#? Please include a simple example. The documentation indicates select or possible selectAll but I can't find an example online or any other SO question which can be pasted in and works.

If possible, give an example which turns the following array {1,2,3,4} into the following {'1a','2a','3a','4a'}. For each element, append "a" to the end, turning it from an Integer to a String.

116772 次浏览

这叫做投影,在 LINQ 中叫做 Select。它不返回一个新数组(就像 JavaScript 的 .map那样) ,而是返回一个 IEnumerable<T>。可以使用 .ToArray将其转换为数组。

using System.Linq; // Make 'Select' extension available
...
var ages = people.Select(person => person.Age).ToArray();

Select works with all IEnumerable<T> which arrays implement. You just need .NET 3.5 and a using System.Linq; statement.

第二个例子是这样的,注意只使用序列中没有数组。

 var items = Enumerable.Range(1, 4).Select(num => string.Format("{0}a", num));

Linq 的 .Selectmap的等价物,而 .Aggregatefold的等价物。

var nums = new[] {1,2,3,4};
string[] r = nums.Select(x => x + "a").ToArray();

集合上的 LINQ 扩展方法为您提供了许多非常方便的实用工具。 Select就是其中之一:

int[] arr = { 1, 2, 3 };
IEnumerable<string> list = arr.Select(el => el + "a");
string[] arr2 = list.ToArray();


foreach (var str in arr2)
Console.Write(str + " ");

这应该会产生:

1a 2a 3a

这可以安全地浓缩为一行程序:

string[] arr2 = arr.Select(el => el + "a").ToArray();

一个实例:

Https://ideone.com/mxxvfy

相关文件:

Enumerable.Select

基本 LINQ 查询操作(C #)

只是为了获取信息,如果 peopleList<Person>,那么 ConvertAll方法就非常类似于 JS 的 map,例如:

var ages = people.ConvertAll<int>(person => person.age);

但是如果你有一个 Array 并且你想使用任何 List<T>方法,你可以很容易地通过将你的变量从 Array 转换成 List 来实现,例如:

var ages = people.ToList().ConvertAll<int>(person => person.age);

最后,如果你真的需要一个 Array,那么你可以把它转换回来,例如:

var ages = people.ToList().ConvertAll<int>(person => person.age).ToArray();

But that last example is not as good as the other answers, and you should use Select if you're working only with Arrays. But if you can, I suggest you to move to List<T>, it's much better!

你可以使用关键字 fromselectinwhile;
Or for your example:

 var ages = (from person in people select person.age).ToArray();

所以基本上语法应该是:

 <<list-output>> = (from <<var-name>> in <<list-input>> select <<operation>>);

如果你不想执行转换到/从一个数组 Array.ConvertAll可以工作。它在 System名称空间的 Array类中声明为:

public static TOutput[] ConvertAll<TInput,TOutput> (TInput[] array, Converter<TInput,TOutput> converter);

例如:

var items = new[] {"abc", "ab", "abcde"};


var result = Array.ConvertAll(items, p => p.Length);
            

Console.Out.WriteLine(string.Join(", ", result)); // Outputs 3, 2, 5

方法参考: https://learn.microsoft.com/en-us/dotnet/api/system.array.convertall?view=net-5.0

检查此部分以查看.NET 版本兼容性: https://learn.microsoft.com/en-us/dotnet/api/system.array.convertall?view=net-5.0#applies-to

// JS
Array.map(item=>{
Console.log(item)
})






// C# ::
Array.ForEach(item =>
Console.Write(item)
)