Trong ví dụ dưới đây, làm cách nào tôi có thể dễ dàng chuyển đổi eventScores
thành List<int>
để tôi có thể sử dụng nó làm thông số cho prettyPrint
?Làm thế nào để chuyển đổi System.Linq.Enumerable.WhereListIterator <int> thành Danh sách <int>?
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); }
pfft , mã chậm! i => (i & 1) == 0 – leppie
Pfft, tối ưu hóa vi mô không được định hướng bằng lược tả. Việc tạo ra trình lặp và bản sao vào danh sách sẽ chậm hơn hàng trăm lần so với bất kỳ khoản tiết kiệm nào được thực hiện bằng cách tối ưu hóa vi toán. * Tối ưu hóa nội dung chậm. * –