18

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); }
Edward Tanguay
  • 189,012
  • 314
  • 712
  • 1,047

3 Answers3

34

You'd use the ToList extension:

var evenScores = scores.Where(i => i % 2 == 0).ToList();
Pete OHanlon
  • 9,086
  • 2
  • 29
  • 28
  • 28
    Pfft, micro-optimizations not driven by profiling. The creation of the iterator and the copy to the list are going to be hundreds of times slower than any savings made by micro-optimizing the math. *Optimize the slow stuff.* – Eric Lippert Oct 08 '09 at 14:35
  • @Eric Lippert - I realize the question - and your comment - are over 10 years old... but.. Q: What were you trying to say? Pete OHanlon's response seems reasonable. Do you disagree? Do you have an alternate suggestion? – FoggyDay Jun 22 '20 at 22:19
  • 1
    @FoggyDay: As my grandmother used to say: take a left at the corner where the red barn used to be. My comment is responding to a deleted comment. And now your comment is responding to a comment on a deleted comment, and my comment is commenting on a comment commenting on a comment commenting on a deleted comment. All these comments should be deleted. – Eric Lippert Jun 23 '20 at 16:55
  • 2
    @FoggyDay: We can reconstruct the content of the deleted comment by my reaction. Likely someone was recommending that `i % 2 == 0` be replaced by `i & 0x1 == 0`. Now, it is true that the DIV instruction that implements mod on x86 introduces several extra nanoseconds of CPU latency than AND does. But the authors of the jitter are aware of this fact! For that to be a perf win then we need to know (1) that the jitter does a bad job, and (2) that saving those nanoseconds of latency is the slowest thing in the program, and (3) the program is unacceptably slow already. – Eric Lippert Jun 23 '20 at 17:14
  • Just checking to make sure you weren't criticizing Pete OHanlon's response. I'm glad that wasn't the case. With regard to the Red Barn - I'm a bit surprised otherwise mature adults would waste any time or brain cells quibbling over the relative efficiency of `i % 2 == 0` vs. `i & 0x1 == 0` ;) – FoggyDay Jun 24 '20 at 06:06
10
var evenScores = scores.Where(i => i % 2 == 0).ToList();

Doesn't work?

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
3

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);
Dzmitry Huba
  • 4,493
  • 20
  • 19
  • 2
    Maybe because `ForEach` is a built-in method on the `List` class. You'd have to write your own extension method to use `ForEach` with `IEnumerable`. – LukeH Oct 08 '09 at 12:47