Edw*_*uay 12 c# linq action func
在下面的例子中,我如何轻松转换eventScores为List<int>可以将其用作参数prettyPrint?
Console.WriteLine("Example of LINQ's Where:");
List<int> scores = new List<int> { 1,2,3,4,5,6,7,8 };
var evenScores = scores.Where(i => i % 2 == 0);
Action<List<int>, string> prettyPrint = (list, title) =>
{
Console.WriteLine("*** {0} ***", title);
list.ForEach(i => Console.WriteLine(i));
};
scores.ForEach(i => Console.WriteLine(i));
prettyPrint(scores, "The Scores:");
foreach (int score in evenScores) { Console.WriteLine(score); }
Run Code Online (Sandbox Code Playgroud)
Pet*_*lon 23
您将使用ToList扩展名:
var evenScores = scores.Where(i => i % 2 == 0).ToList();
Run Code Online (Sandbox Code Playgroud)
var evenScores = scores.Where(i => i % 2 == 0).ToList();
Run Code Online (Sandbox Code Playgroud)
不起作用?
| 归档时间: |
|
| 查看次数: |
30015 次 |
| 最近记录: |