c# - 如何将 System.Linq.Enumerable.WhereListIterator<int> 转换为 List<int>?

标签 c# linq action func

在下面的示例中,我如何轻松转换 eventScoresList<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); }

最佳答案

您将使用 ToList 扩展:

var evenScores = scores.Where(i => i % 2 == 0).ToList();

关于c# - 如何将 System.Linq.Enumerable.WhereListIterator<int> 转换为 List<int>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1537528/

相关文章:

c# - 我怎样才能只存储日期时间的日期部分。今天属性到 SQL Server 表日期时间列?

c# - 实现一个 session 并从 mysql 表中检索?

c# - 使用 QueryPerformanceFrequency() 和 QueryPerformanceCounter() 将我的 C++ 代码转换为 C#?

java - Java 中窗口框架关闭时不执行任何操作

c# - 在 ASP.NET MVC 中通过重定向传递复杂对象?

c# - iTextSharp 文件操作和显示到 FileStreamResults

c# - LINQ:转换为值类型 'System.Int32' 失败,因为具体化值为 null

c# - 将列表转换为字符串列表

c# - LINQ 中的 ISNULL 等价物

c# - 将 Action<T> 回调转换为等待