How to convert System.Linq.Enumerable.WhereListIterator<int> to List<int>?
Solution 1
You'd use the ToList extension:
var evenScores = scores.Where(i => i % 2 == 0).ToList();
Solution 2
var evenScores = scores.Where(i => i % 2 == 0).ToList();
Doesn't work?
Solution 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);
Related videos on Youtube
Angry Dan
web/software developer, .NET, C#, WPF, PHP, software trainer, English teacher, have philosophy degree, love languages, run marathons my tweets: http://www.twitter.com/edward_tanguay my runs: http://www.tanguay.info/run my code: http://www.tanguay.info/web my publications: PHP 5.3 training video (8 hours, video2brain) my projects: http://www.tanguay.info
Updated on July 09, 2022Comments
-
Angry Dan almost 2 years
In the below example, how can I easily convert
eventScores
toList<int>
so that I can use it as a parameter forprettyPrint
?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); }
-
LukeH over 14 yearsMaybe because
ForEach
is a built-in method on theList<T>
class. You'd have to write your own extension method to useForEach
withIEnumerable<T>
. -
Eric Lippert over 14 yearsPfft, 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.
-
FoggyDay almost 4 years@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?
-
Eric Lippert almost 4 years@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 almost 4 years@FoggyDay: We can reconstruct the content of the deleted comment by my reaction. Likely someone was recommending that
i % 2 == 0
be replaced byi & 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. -
FoggyDay almost 4 yearsJust 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
;)