Consider the following common JavaScript construct
考虑以下常见的JavaScript构造
var ages = people.map(person => person.age);
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.
在C#中,这相当于什么?请举一个简单的例子。文档表明选择或可能selectAll但我找不到在线示例或任何其他可以粘贴和工作的SO问题。
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.
如果可能的话,举一个例子,将下面的数组{1,2,3,4}变成下面的{'1a','2a','3a','4a'}。对于每个元素,在末尾附加“a”,将其从Integer转换为String。
5 个解决方案
#1
46
This is called projection which is called Select
in LINQ. That does not return a new array (like how JavaScript's .map
does), but an IEnumerable<T>
. You can convert it to an array with .ToArray
.
这称为投影,在LINQ中称为Select。这不会返回一个新数组(就像JavaScript的.map一样),而是一个IEnumerable
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.
选择适用于所有IEnumerable
For your 2nd example use something like this. Notice there are no arrays in use - only sequences.
对于你的第二个例子,使用这样的东西。请注意,没有使用数组 - 只有序列。
var items = Enumerable.Range(1, 4).Select(num => string.Format("{0}a", num));
#2
5
Only for info, if people
is a List<Person>
, the ConvertAll
method is pretty similar to JS's map
, e.g:
仅对于info,如果people是List
var ages = people.ConvertAll<int>(person => person.age);
But if you have an Array and you want to use any List<T>
methods, you can easily achieve that by converting your variable into a List from Array, e.g:
但是如果你有一个数组并且想要使用任何List
var ages = people.ToList().ConvertAll<int>(person => person.age);
And finally, if you really need an Array back, then you could convert it back, e.g:
最后,如果你确实需要一个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!
但是最后一个例子并不像其他答案那么好,如果你只使用Arrays,你应该使用Select。但如果可以的话,我建议你转到List
#3
1
Linq's .Select
is the map
equivalent and .Aggregate
is the fold
equivalent.
Linq的.Select是地图的等价物.Aggregate是等效的折叠。
var nums = new[] {1,2,3,4};
string[] r = nums.Select(x => x + "a").ToArray();
#4
1
The LINQ extension methods on collections give you a host of really handy utilities. Select
is one of them:
集合上的LINQ扩展方法为您提供了许多非常方便的实用程序。选择就是其中之一:
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 + " ");
This should output:
这应输出:
1a 2a 3a
This can safely be condensed to a 1-liner:
这可以安全地浓缩为1-liner:
string[] arr2 = arr.Select(el => el + "a").ToArray();
A working example:
一个工作的例子:
https://ideone.com/mxxvfy
Related docs:
相关文档:
Enumerable.Select
Basic LINQ Query Operations (C#)
基本LINQ查询操作(C#)
#5
0
You can use the keywords from
, select
, in
and while
;
Or for your example:
您可以使用关键字from,select,in和while;或者为你的例子:
var ages = (from person in people select person.age).ToArray();
So essentially the syntax would be:
所以基本上语法是:
<<list-output>> = (from <<var-name>> in <<list-input>> select <<operation>>);
#1
46
This is called projection which is called Select
in LINQ. That does not return a new array (like how JavaScript's .map
does), but an IEnumerable<T>
. You can convert it to an array with .ToArray
.
这称为投影,在LINQ中称为Select。这不会返回一个新数组(就像JavaScript的.map一样),而是一个IEnumerable
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.
选择适用于所有IEnumerable
For your 2nd example use something like this. Notice there are no arrays in use - only sequences.
对于你的第二个例子,使用这样的东西。请注意,没有使用数组 - 只有序列。
var items = Enumerable.Range(1, 4).Select(num => string.Format("{0}a", num));
#2
5
Only for info, if people
is a List<Person>
, the ConvertAll
method is pretty similar to JS's map
, e.g:
仅对于info,如果people是List
var ages = people.ConvertAll<int>(person => person.age);
But if you have an Array and you want to use any List<T>
methods, you can easily achieve that by converting your variable into a List from Array, e.g:
但是如果你有一个数组并且想要使用任何List
var ages = people.ToList().ConvertAll<int>(person => person.age);
And finally, if you really need an Array back, then you could convert it back, e.g:
最后,如果你确实需要一个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!
但是最后一个例子并不像其他答案那么好,如果你只使用Arrays,你应该使用Select。但如果可以的话,我建议你转到List
#3
1
Linq's .Select
is the map
equivalent and .Aggregate
is the fold
equivalent.
Linq的.Select是地图的等价物.Aggregate是等效的折叠。
var nums = new[] {1,2,3,4};
string[] r = nums.Select(x => x + "a").ToArray();
#4
1
The LINQ extension methods on collections give you a host of really handy utilities. Select
is one of them:
集合上的LINQ扩展方法为您提供了许多非常方便的实用程序。选择就是其中之一:
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 + " ");
This should output:
这应输出:
1a 2a 3a
This can safely be condensed to a 1-liner:
这可以安全地浓缩为1-liner:
string[] arr2 = arr.Select(el => el + "a").ToArray();
A working example:
一个工作的例子:
https://ideone.com/mxxvfy
Related docs:
相关文档:
Enumerable.Select
Basic LINQ Query Operations (C#)
基本LINQ查询操作(C#)
#5
0
You can use the keywords from
, select
, in
and while
;
Or for your example:
您可以使用关键字from,select,in和while;或者为你的例子:
var ages = (from person in people select person.age).ToArray();
So essentially the syntax would be:
所以基本上语法是:
<<list-output>> = (from <<var-name>> in <<list-input>> select <<operation>>);