If I have a very large collection of IEnumerable , in the order of millions of objects, that would be too large to load everything at once. Collections are returned by the yield method:
private static IEnumerable<MyData> ExtractModelsFromDb() { using (DbDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { Load MyData yield return MyData; } reader.Close(); } }
This data is consumed in one Foreach cycle:
public void RunStuff() { var myCollection = ExtractModelsFromDb(); foreach (var data in myCollection) {
I know that a collection under IEnumerable loaded one object at a time, but objects are deleted after they are used in Foreach or they remain in the collection until Foreach complete.
source share