これは遅延実行によるものだと思いますが、 List.AddRange 関数が追加される項目を列挙する IEnumerable を受け入れると考えたでしょう。
します。短絡がICollection<T>
あります (この場合はヒットしません)。これによりICollection<T>.CopyTo
、項目を列挙する代わりに使用されますが、それ以外の場合はコレクションが列挙されます。
実際の例については、次を試してください。
using System;
using System.Linq;
using System.Collections.Generic;
internal class Program
{
private static List<T> RunQuery<T>(IEnumerable<T> someCollection, Func<T, bool> predicate)
{
List<T> items = new List<T>();
IEnumerable<T> addItems = someCollection.Where(predicate);
items.AddRange(addItems);
return items;
}
static void Main()
{
var values = Enumerable.Range(0, 1000);
List<int> results = RunQuery(values, i => i >= 500);
Console.WriteLine(results.Count);
Console.WriteLine("Press key to exit:");
Console.ReadKey();
}
}
これはあなたの正確なコードを使用し、500 ( 内の適切なアイテム数List<T>
) を出力します。