int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
string[] strings = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
var textNums =
from n in numbers
select strings[n];
Console.WriteLine("Number strings:");
foreach (var s in textNums)
{
Console.WriteLine(s);
}
如何使用Lambda Expression编写相同的代码....
答案 0 :(得分:3)
numbers.ToList().ForEach(item => Console.WriteLine(strings[item]));
答案 1 :(得分:2)
我假设你的意思只是“来自” - 陈述?:
var textNs = numbers.Select(i => strings[i]);
答案 2 :(得分:1)
var result=numbers.Select(n=>strings[n]);
这是正确的,但结果属于IEnumerable< String >
类型
此处,n
的左侧=>(lambda operator)
表示传递给函数的参数,strings[n]
的右侧=>
表示函数体。
有关Linq&amp ;;的更多信息lambda表达式参考:http://www.codeproject.com/Articles/33769/Basics-of-LINQ-Lamda-Expressions
答案 3 :(得分:0)
var result = numbers.Select(n => strings[n]);