Chain queries example (C#) (LINQ to XML)
This example builds on the example in Deferred execution example and shows what happens when you chain together two queries that both use deferred execution and lazy evaluation.
Example: Add a second extension method that uses yield return
to defer execution
In this example, another extension method is introduced, AppendString
, which appends a specified string onto every string in the source collection, and then yields the changed string.
public static class LocalExtensions
{
public static IEnumerable<string>
ConvertCollectionToUpperCase(this IEnumerable<string> source)
{
foreach (string str in source)
{
Console.WriteLine("ToUpper: source >{0}<", str);
yield return str.ToUpper();
}
}
public static IEnumerable<string>
AppendString(this IEnumerable<string> source, string stringToAppend)
{
foreach (string str in source)
{
Console.WriteLine("AppendString: source >{0}<", str);
yield return str + stringToAppend;
}
}
}
class Program
{
static void Main(string[] args)
{
string[] stringArray = { "abc", "def", "ghi" };
IEnumerable<string> q1 =
from s in stringArray.ConvertCollectionToUpperCase()
select s;
IEnumerable<string> q2 =
from s in q1.AppendString("!!!")
select s;
foreach (string str in q2)
{
Console.WriteLine("Main: str >{0}<", str);
Console.WriteLine();
}
}
}
This example produces the following output:
ToUpper: source >abc<
AppendString: source >ABC<
Main: str >ABC!!!<
ToUpper: source >def<
AppendString: source >DEF<
Main: str >DEF!!!<
ToUpper: source >ghi<
AppendString: source >GHI<
Main: str >GHI!!!<
In this example, you can see that each extension method operates one at a time for each item in the source collection.
What should be clear from this example is that even though we have chained together queries that yield collections, no intermediate collections are materialized. Instead, each item is passed from one lazy method to the next. This results in a much smaller memory footprint than an approach that would first take one array of strings, then create a second array of strings that have been converted to uppercase, and finally create a third array of strings where each string has the exclamation points appended to it.
The next article in this tutorial illustrates intermediate materialization: