如何将 System.Linq.Enumerable.WhereListIterator转换为列表

发布于 2024-08-06 14:11:04 字数 640 浏览 2 评论 0原文

在下面的示例中,如何轻松地将 eventScores 转换为 List 以便将其用作 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); }

In the below example, how can I easily convert eventScores to List<int> so that I can use it as a parameter for 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); }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

愚人国度 2024-08-13 14:11:04

您可以使用 ToList 扩展:

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

You'd use the ToList extension:

var evenScores = scores.Where(i => i % 2 == 0).ToList();
哑剧 2024-08-13 14:11:04
var evenScores = scores.Where(i => i % 2 == 0).ToList();

不起作用?

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

Doesn't work?

留蓝 2024-08-13 14:11:04

顺便问一下,为什么您要为 Scores 参数声明这样特定类型的 PrettyPrint,而不是仅将此参数用作 IEnumerable (我假设这就是您实现 ForEach 扩展方法的方式)?那么为什么不改变 PrettyPrint 签名并保持这个惰性评估呢? =)

像这样:

Action<IEnumerable<int>, string> prettyPrint = (list, title) =>
{
    Console.WriteLine("*** {0} ***", title);
    list.ForEach(i => Console.WriteLine(i));
};

prettyPrint(scores.Where(i => i % 2 == 0), "Title");

更新:

或者您可以避免像这样使用List.ForEach(不考虑字符串连接效率低下):

var text = scores.Where(i => i % 2 == 0).Aggregate("Title", (text, score) => text + Environment.NewLine + score);

By the way why do you declare prettyPrint with such specific type for scores parameter and than use this parameter only as IEnumerable (I assume this is how you implemented ForEach extension method)? So why not change prettyPrint signature and keep this lazy evaluated? =)

Like this:

Action<IEnumerable<int>, string> prettyPrint = (list, title) =>
{
    Console.WriteLine("*** {0} ***", title);
    list.ForEach(i => Console.WriteLine(i));
};

prettyPrint(scores.Where(i => i % 2 == 0), "Title");

Update:

Or you can avoid using List.ForEach like this (do not take into account string concatenation inefficiency):

var text = scores.Where(i => i % 2 == 0).Aggregate("Title", (text, score) => text + Environment.NewLine + score);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文