{"id":1044,"date":"2010-12-01T00:00:00","date_gmt":"2010-12-01T00:00:00","guid":{"rendered":"https:\/\/test.simple-talk.com\/uncategorized\/linq-secrets-revealed-chaining-and-debugging\/"},"modified":"2021-05-17T18:36:28","modified_gmt":"2021-05-17T18:36:28","slug":"linq-secrets-revealed-chaining-and-debugging","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/development\/dotnet-development\/linq-secrets-revealed-chaining-and-debugging\/","title":{"rendered":"LINQ Secrets Revealed: Chaining and Debugging"},"content":{"rendered":"<div class=\"note\">\n<p><strong>Note (2016.12.01):<\/strong><br \/>\n Exactly six years later almost all the information in this article is still good&#8211;not an easy feat with the pace of software change! But now there is more to say, so I have written a sequel to this article expounding upon the capabilities of a powerful new LINQ debugging extension for Visual Studio. You might want to continue reading this article first though, to get a good grounding (you know, walk before you can run). Then go read <a href=\"https:\/\/www.simple-talk.com\/dotnet\/net-development\/linq-debugging-visualization\/\">LINQ Debugging and Visualization<\/a>.<\/p>\n<\/div>\n<div id=\"pretty\">\n<h2>Contents<\/h2>\n<ul>\n<li><a href=\"#first\">Pipelines and Method Chaining<\/a>\n<ul>\n<li><a href=\"#second\">The Unix Origins: Command Pipelining<\/a><\/li>\n<li><a href=\"#third\">.NET Equivalent: Method Chaining<\/a><\/li>\n<li><a href=\"#fourth\">.NET Pipelines with LINQ<\/a><\/li>\n<li><a href=\"#fifth\">Breakpoints in a LINQ Chain<\/a><\/li>\n<\/ul>\n<\/li>\n<li><a href=\"#sixth\">Debugging Unbound<\/a>\n<ul>\n<li><a href=\"#seventh\">Simple Debugging: Injecting a NOP<\/a><\/li>\n<li><a href=\"#eighth\">Advanced Debugging: Injecting a Watcher<\/a><\/li>\n<li><a href=\"#ninth\">Dump Method in Visual Studio: Points to Take Away<\/a><\/li>\n<\/ul>\n<\/li>\n<li><a href=\"#tenth\">LINQPad: Visualizing Your Data<\/a>\n<ul>\n<li><a href=\"#eleventh\">Basic LINQPad<\/a><\/li>\n<li><a href=\"#twelveth\">LINQPad with Method Chaining<\/a><\/li>\n<li><a href=\"#thirteenth\">Examining Complex Objects<\/a><\/li>\n<li><a href=\"#fourteenth\">There and Back Again<\/a><\/li>\n<\/ul>\n<\/li>\n<li><a href=\"#fifteenth\">Conclusion<\/a><\/li>\n<li><a href=\"#sixteenth\">Footnotes<\/a><\/li>\n<\/ul>\n<p class=\"start\">This article is for .NET developers who have not used LINQ, LINQ users who have not used LINQPad, and LINQPad users who have not used LINQPad Visualizer. (Some familiarity with LINQ is assumed though.) I take you beyond the basic concept of a LINQ query and reveal the simple techniques for creating LINQ chains, softly introducing the notion with analogous ideas in Unix and in .NET programming in general. As soon as you think about chaining, though, you have to be concerned about how to keep the &#8220;stuff&#8221; in the middle accessible, to keep it from becoming an opaque black box. I show you how to do this both in Visual Studio with a simple extension method and in LINQPad with its powerful <b>Dump<\/b> method. The accompanying code archive[1] lets you experiment with everything discussed as you read along.<\/p>\n<h1 id=\"first\">Pipelines and Method Chaining<\/h1>\n<h2 id=\"second\">The Unix Origins: Command Pipelining<\/h2>\n<p>The concept of a software pipeline is not a C# innovation. In fact, it is not new at all. It is not even recent. Pipelines appeared in 1972 in Unix, following on Douglas McIlroy&#8217;s <a href=\"http:\/\/doc.cat-v.org\/unix\/pipes\/\">1964 proposal<\/a>. This example of a Unix pipeline (from the Wikipedia entry on <a href=\"http:\/\/en.wikipedia.org\/wiki\/Pipeline_%28Unix%2529\">pipelines<\/a>) implements a simplistic, command-line spellchecker on Unix\/Linux systems. This pipeline runs 7 independent applications. Each application ties its output to the input of the next application using the pipe (|) symbol. To tie the whole package together, the first application, curl, obtains its input from the web page supplied as an argument. The last application, less, feeds its output to the console where the user may view it. (The steps in between massage the data to identify, isolate, and sort individual words, then compare them to a reference dictionary.)<\/p>\n<pre class=\"lang:c# theme:vs2012\">curl \"http:\/\/en.wikipedia.org\/wiki\/Pipeline_(Unix)\"\u00a0| \\\r\nsed 's\/[^a-zA-Z ]\/ \/g'\u00a0| \\\r\ntr 'A-Z '\u00a0'a-z\\n'\u00a0| \\\r\ngrep '[a-z]'\u00a0| \\\r\nsort -u\u00a0| \\\r\ncomm -23\u00a0- \/usr\/share\/dict\/words | \\\r\nless\r\n<\/pre>\n<h2 id=\"third\">.NET Equivalent: Method Chaining<\/h2>\n<p>Fast forward and shift to the .NET environment. This next example illustrates <a href=\"http:\/\/en.wikipedia.org\/wiki\/Method_chaining\">method chaining<\/a>, the code-level equivalent of application pipelining. You have almost certainly used method chaining but may not have seen the term before. Here you start with a whimsical string, swap parts, shrink it, chop it up, and finally write out its pieces. (This code is available in the StringMethodChaining project of the ChainingAndDebugging solution (VS2010) in the accompanying code archive.)<\/p>\n<pre class=\"lang:c# theme:vs2012\">using System;  using System.Linq;\r\n\r\nnamespace StringMethodChaining\r\n{\r\n\u00a0\u00a0\u00a0 class Program\r\n\u00a0\u00a0\u00a0 {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 static void Main(string[] args)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \"aardvarks\u00a0 AND\u00a0 antelopes AND hippopotami\"\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 .Replace(\"antelopes\", \"centipedes\")\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 .Substring(\"aardvarks\".Length)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 .ToLower()\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 .Trim()\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 .Split(new[] {\"and\"}, StringSplitOptions.None)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 .ToList()\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 .ForEach(item =&gt; Console.WriteLine(item.Trim()));\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Console.ReadLine();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0 }\r\n}\r\n<\/pre>\n<p>The basic principle to observe from these cosmetically different examples is the same: connect building blocks together where the output type of one corresponds to the input type of the next. In the Unix\/Linux case, command line applications typically use a text stream for input and generate a text stream for output. This allows you to connect any two components together. The C# case is rather more complicated on the surface because there is no &#8220;universal&#8221; input\/output format. Rather you are free to define methods with arbitrary types for input and output to suit your needs. To create a pipeline, then, it is a simple matter of <i>impedance matching<\/i>[2]. Here are the methods used, explicitly showing their inputs and outputs. Note how the output of each method in the chain matches the input requirement of the next method:<\/p>\n<p class=\"illustration\"><img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1194-Sorens1.jpg\" alt=\"1194-Sorens1.jpg\" \/><\/p>\n<p>Note that you could write the same code without chaining, by introducing a slew of temporary variables.<\/p>\n<pre class=\"lang:c# theme:vs2012\">string s1 = \"aardvarks\u00a0 AND\u00a0 antelopes AND hippopotami\";\r\nstring s2 = s1.Replace(\"antelopes\", \"centipedes\");\r\nstring s3 = s2.Substring(\"aardvarks\".Length);\r\nstring s4 = s3.ToLower();\r\nstring s5 = s4.Trim();\r\nstring[] strings = s5.Split(new[] { \"and\" }, StringSplitOptions.None);\r\nList&lt;string&gt; list = strings.ToList();\r\nlist.ForEach(item =&gt; Console.WriteLine(item.Trim()));\r\nConsole.ReadLine();\r\n<\/pre>\n<p>This code does the same thing but takes significantly more effort to comprehend. The previous code you could likely understand almost with a glance. Here, you have to stare at it for a bit. So is method chaining always better? Actually, no. If you want to debug this code and view an intermediate value, you cannot do it with pure method chaining. If you set a breakpoint and then execute, Figure 1 shows what you see when you land on the breakpoint.<\/p>\n<p class=\"illustration\"><img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1194-Sorens2.jpg\" alt=\"1194-Sorens2.jpg\" \/><\/p>\n<p class=\"caption\">Figure 1 Breakpoint on a Method Chain<\/p>\n<p>The entire chain is considered an indivisible unit! That is because, well, it is. Remember that Visual Studio breaks on <i>statements<\/i> and any single method in the chain is not a statement in and of itself. From this breakpoint if you use the <b>step over<\/b> command, you advance to line 19, the <b>Console.ReadLine<\/b>. However, if you instead use the <b>step into<\/b> command, you advance to the <b>Console.WriteLine<\/b> on line 18, because though deeply embedded, <b>Console.WriteLine<\/b> is a full-fledged statement. Unfortunately, if you need to see any intermediate value other than <b>item<\/b> in that line, you have to rewrite the code to introduce separate statements with temporary variables.<\/p>\n<p>The principled, high-minded designer in you is, I am sure, repulsed by such an unsatisfactory kludge. Fear not, for there is a better way. But first, take a look at method chaining as it applies to LINQ.<\/p>\n<h2 id=\"fourth\">.NET Pipelines with LINQ<\/h2>\n<p>The vast majority of articles on LINQ[3] introduce it with a simple query like this:<\/p>\n<pre class=\"lang:c# theme:vs2012\">int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };  var lowNums =\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 from n in numbers\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 where n &lt; 5\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 select n;\r\n<\/pre>\n<p>They explain that you specify your data source with a <b>from<\/b> clause, filter it with a <b>where<\/b> clause, and project it to the desired result with a <b>select<\/b> clause. They then go on to show how to use that query typically with a <b>foreach<\/b> loop like this:<\/p>\n<pre class=\"lang:c# theme:vs2012\">Console.WriteLine(\"Numbers &lt; 5:\");  foreach (var x in lowNums)\r\n{\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Console.WriteLine(x);\r\n}\r\n<\/pre>\n<p>Even articles discussing more advanced LINQ methods typically exhibit a simple example, which is fine and necessary, but almost always stop after proclaiming that you conclude your LINQ expression with a <b>Select<\/b> or <b>GroupBy<\/b>. Even the useful and venerable <a href=\"http:\/\/msdn.microsoft.com\/en-us\/vcsharp\/aa336746.aspx\">101 LINQ Samples<\/a> page from Microsoft shows only the simplest examples, yielding no clue about method chaining.<\/p>\n<p>LINQ Queries may appear in one of two forms; the query above is written using <i>query syntax.<\/i> The next example uses <i>method syntax<\/i> (also called <i>lambda syntax<\/i>). The two forms are exactly equivalent (where they overlap), and performance is also exactly the same because, during compilation, query syntax expressions are converted to lambda syntax internally. However, the lambda syntax is richer, particularly in C#[4].<\/p>\n<p>When it comes to method chaining, I prefer to use lambda syntax. This next example uses it to illustrate a real-world LINQ example. This code comes straight out of the HostSwitcher application that I built and discussed at length in my recent article, <a href=\"http:\/\/www.simple-talk.com\/dotnet\/.net-framework\/creating-tray-applications-in-.net-a-practical-guide\/\">Creating Tray Applications in .NET: A Practical Guide<\/a>. HostSwitcher lets you re-route entries in your hosts file with a single click on the context menu attached to the icon in the system tray. The application is heavily LINQ-centric. One key portion of code takes your hosts file (read into an array) and uses LINQ to convert it to a dictionary that is later consumed by other segments of the code to generate a context menu among other uses. The <b>CreateMap<\/b> method generates the projectDict dictionary:<\/p>\n<pre class=\"lang:c# theme:vs2012\">private void CreateMap()  {\r\n\u00a0\u00a0\u00a0 projectDict = \r\n\u00a0\u00a0\u00a0 hostFileData\r\n\u00a0\u00a0\u00a0 .Select(line =&gt; new { line, m = FilteringRegex.Match(line) })\r\n\u00a0\u00a0\u00a0 .Where(item =&gt; item.m.Success)\r\n\u00a0\u00a0\u00a0 .Select(item =&gt; new\r\n\u00a0\u00a0\u00a0 {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 item.line,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 item.m,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 project = item.m.Groups[ProjectSubGroupIndex].ToString().Trim(),\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 serverGroup = item.m.Groups[ServerGroupSubGroupIndex].ToString().Trim()\r\n\u00a0\u00a0\u00a0 })\r\n\u00a0\u00a0\u00a0 .GroupBy(item =&gt; new { item.project, item.serverGroup }, item =&gt; item.line)\r\n\u00a0\u00a0\u00a0 .GroupBy(projAndServer =&gt; projAndServer.Key.project)\r\n\u00a0\u00a0\u00a0 .ToDictionary(\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 project =&gt; project.Key,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 project =&gt; project.Select(item =&gt;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 new ServerGroup\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Name = item.Key.serverGroup,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 EnabledCount\u00a0 = item.Where(line =&gt; !IsCommented(line)).Count(),\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 DisabledCount = item.Where(line =&gt;\u00a0 IsCommented(line)).Count()\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 )\r\n\u00a0\u00a0\u00a0 );\r\n}\r\n<\/pre>\n<p>To understand method chaining with LINQ, consider the inputs and outputs of the LINQ methods in the above chain:<\/p>\n<p class=\"illustration\"><img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1194-Sorens35.JPG\" alt=\"1194-Sorens35.JPG\" \/><\/p>\n<p>Observe that LINQ has a great affinity for IEnumerable&lt;T&gt; objects; many LINQ methods fit this footprint:<\/p>\n<p class=\"illustration\"><img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1194-Sorens4.jpg\" alt=\"1194-Sorens4.jpg\" \/><\/p>\n<p>Therefore, LINQ naturally lends itself to method chaining! Kris Thompson&#8217;s blog contains a great reference of LINQ operators, identifying the return values of each so you can see at a glance which ones lend themselves to LINQ chaining. Many of them-with IEnumerable&lt;T&gt; as both input and output-may be used at any position in a chain. But since all (?almost all) LINQ operators use IEnumerable&lt;T&gt; as input, all of them may be used at the end of the chain[5].<\/p>\n<h2 id=\"fifth\">Breakpoints in a LINQ Chain<\/h2>\n<p>The final point here is that LINQ method chaining is different than normal method chaining with respect to stepping in the debugger. Though the entire chain is marked with a single breakpoint, once you reach the breakpoint you <i>can<\/i> step through a LINQ query. Figure 2 shows the scenario after having pressed the <b>step over<\/b> button a number of times. At that point, you can inspect local variables as on any breakpoint with the Immediate window, tooltips, etc. It is not that the methods are special in any sense as compared to the string methods you saw earlier. Rather, it is the method arguments that are different. A LINQ method typically takes a <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb397687.aspx\">lambda expression<\/a>, which is an anonymous function composed of expressions <i>and statements<\/i>. Thus, you may step onto these statements with the debugger as well.<\/p>\n<p class=\"illustration\"><img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1194-Sorens5.jpg\" alt=\"1194-Sorens5.jpg\" \/><\/p>\n<p class=\"caption\">Figure 2 Breakpoint on a LINQ Chain<\/p>\n<p>Actually setting breakpoints on parts of the LINQ chain, however, is quirky. If you use the shortcut key (F9) pressing it once sets a breakpoint on the entire chain. Pressing it again, removes it. Repeat ad infinitum. If, instead you use the mouse to set a breakpoint by clicking on the grey channel at the left edge of the window the first click will perform the same (setting a breakpoint on the entire chain) independent of which line in the chain your mouse is adjacent to. I find, though, that if I stubbornly click in the channel adjacent to different lines within the chain I can <i>sometimes<\/i> get a breakpoint to stick.<\/p>\n<p>Thus far you have seen how attempts at debugging method chains are useful to a degree, but still unsatisfactory. The next section shows you some powerful remedies.<\/p>\n<h1 id=\"sixth\">Debugging Unbound<\/h1>\n<h2 id=\"seventh\">Simple Debugging: Injecting a NOP<\/h2>\n<p>The first technique to allow setting breakpoints inside a LINQ method chain is to add a <a href=\"http:\/\/en.wikipedia.org\/wiki\/NOP\">nop<\/a>: a statement that does nothing, but a <i>statement<\/i> is what you need! In LINQ a nop consists of a lambda expression that performs an identity transformation, but you want it to use a statement rather than an expression, i.e. this:<\/p>\n<pre>z =&gt; { return z; }<\/pre>\n<p>&#8230;rather than this:<\/p>\n<pre>z =&gt; z<\/pre>\n<p>The other crucial factor is that the statement must be accessible to the debugger, i.e. it must be on a line by itself. Then you can set a reliable breakpoint, as shown in Figure 3.<\/p>\n<p class=\"illustration\"><img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1194-Sorens6.jpg\" alt=\"1194-Sorens6.jpg\" \/><\/p>\n<p class=\"caption\">Figure 3 Adding Breakpoints with Embedded Statements<\/p>\n<p>(Thanks to Eric White&#8217;s blog entry <a href=\"http:\/\/blogs.msdn.com\/b\/ericwhite\/archive\/2008\/11\/07\/debugging-linq-queries.aspx\">Debugging LINQ Queries<\/a> for this tip.)<\/p>\n<h2 id=\"eighth\">Advanced Debugging: Injecting a Watcher<\/h2>\n<p>In the previous section you learned to inject a simple inline expression. That worked because, being wrapped in a Select predicate, it still fits the classic LINQ signature:<\/p>\n<p class=\"illustration\"><img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1194-Sorens7.jpg\" alt=\"1194-Sorens7.jpg\" \/><\/p>\n<p>That technique has its uses but to do anything non-trivial it is more useful to encapsulate a diagnostic routine into a separate package. To explore this avenue, consider this simple LINQ query to perform some trivial string operations. (The code in this section is available in the LinqMethodChaining project of the ChainingAndDebugging solution (VS2010) in the accompanying code archive.)<\/p>\n<pre class=\"lang:c# theme:vs2012\">private static IEnumerable&lt;string&gt; ProcessWordList1(string[] words)  {\r\n\u00a0\u00a0\u00a0\u00a0 return words\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 .Select(word =&gt; word.Trim())\r\n\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 .Select(word =&gt; word.ToLower())\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 .Where(word =&gt; word.StartsWith(\"k\"))\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 .OrderBy(word =&gt; word);\r\n}\r\n<\/pre>\n<p>The input is this word list, which includes some different casings, some extraneous spaces, and is unordered.<\/p>\n<pre class=\"lang:c# theme:vs2012\">static readonly string[] Words = new string[]  {\"\u00a0\u00a0 KOOKABURRA\", \"Frogmouth\", \"kingfisher\u00a0\u00a0 \", \"loon\", \"merganser\"};\r\n<\/pre>\n<p>The program to wrap around these pieces is just:<\/p>\n<pre class=\"lang:c# theme:vs2012\">static void Main(string[] args)  {\r\n\u00a0\u00a0\u00a0 var newWords = ProcessWordList1(Words);\r\n\u00a0\u00a0\u00a0 foreach (var word in newWords) { Console.WriteLine(word); }\r\n\u00a0\u00a0\u00a0 Console.ReadLine();\r\n}\r\n<\/pre>\n<p>The output from this is just these two birds: <b>kingfisher<\/b> followed by <b>kookaburra<\/b>. This example is deliberately simple but in the following discussion assume you have something more elaborate where the machinations it performs are non-obvious. To be able to examine the innards of the LINQ chain, create a new class to contain an extension method based on the <b>Watch<\/b> method in Bart De Smet&#8217;s informative article <a href=\"http:\/\/community.bartdesmet.net\/blogs\/bart\/archive\/2009\/04\/23\/linq-to-objects-debugging.aspx\">LINQ to Objects &#8211; Debugging<\/a>. I have enhanced his extension method to support multiple colors instead of a single color, and to show invisible characters for illustration. (I have also chosen to rename it from <b>Watch<\/b> to <b>Dump<\/b> to be consistent with subsequent portions of this article.) Here is my version:<\/p>\n<pre class=\"lang:c# theme:vs2012\">public static class EnumerableDebugger\r\n{\r\n\u00a0\u00a0\u00a0 public static ConsoleColor DefaultColor = ConsoleColor.Yellow;\r\n\u00a0\u00a0\u00a0 public static bool ShowWhiteSpace { get; set; }\r\n\r\n\u00a0\u00a0\u00a0 public static IEnumerable&lt;T&gt; Dump&lt;T&gt;(this IEnumerable&lt;T&gt; input)\r\n\u00a0\u00a0\u00a0 {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return Dump(input,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 item =&gt; item != null ? item.ToString() : \"(null)\", DefaultColor);\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \r\n\u00a0\u00a0\u00a0 public static IEnumerable&lt;T&gt; Dump&lt;T&gt;(\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 this IEnumerable&lt;T&gt; input,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ConsoleColor consoleColor)\r\n\u00a0\u00a0\u00a0 {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return Dump(input,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 item =&gt; item != null ? item.ToString() : \"(null)\", consoleColor);\r\n\u00a0\u00a0\u00a0 }\r\n\r\n\u00a0\u00a0\u00a0 public static IEnumerable&lt;T&gt; Dump&lt;T&gt;(\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 this IEnumerable&lt;T&gt; input,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Func&lt;T, string&gt; toString,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ConsoleColor consoleColor)\r\n\u00a0\u00a0\u00a0 {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 foreach (var item in input)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Console.ForegroundColor = consoleColor;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Console.WriteLine(\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ShowWhiteSpace ? '[' + toString(item) + ']' : toString(item));\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Console.ResetColor();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 yield return item;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0 }\r\n}\r\n<\/pre>\n<p>This extension method adds color-coded diagnostic output intermixed with your program&#8217;s normal output. More importantly, it performs an identity transformation on its input just like the previous nop technique: that is, <i>it returns its input unchanged<\/i>. Because of this, it is safe to inject this into the LINQ chain anywhere you like. Here is the method instrumented with <b>Dump<\/b> calls injected after every LINQ operation:<\/p>\n<pre class=\"lang:c# theme:vs2012\">private static IEnumerable&lt;string&gt; ProcessWordList2(string[] words)  {\r\n\u00a0\u00a0\u00a0 EnumerableDebugger.ShowWhiteSpace = true;\r\n\u00a0\u00a0\u00a0 return words\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 .Dump()\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 .Select(word =&gt; word.Trim())\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 .Dump()\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 .Select(word =&gt; word.ToLower())\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 .Dump()\r\n\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0.Where(word =&gt; word.StartsWith(\"k\"))\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 .Dump()\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 .OrderBy(word =&gt; word)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 .Dump();\r\n}\r\n<\/pre>\n<p>The output of the program is shown in Figure 4, left side. You can distinguish the program output from the diagnostic output in yellow but it is impossible to distinguish the multiple occurrences in yellow. By specifying non-default arguments to <b>Dump<\/b> you can enhance the output. The final version of ProcessWordList below uses the same <b>Dump<\/b> extension method but this time supplies two arguments, one to label the step and one to colorize the step. This method yields the output in Figure 4, right side.<\/p>\n<pre class=\"lang:c# theme:vs2012\">private static IEnumerable&lt;string&gt; ProcessWordList3(string[] words)  {\r\n\u00a0\u00a0\u00a0 EnumerableDebugger.ShowWhiteSpace = true;\r\n\u00a0\u00a0\u00a0 return words\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 .Dump(w =&gt; \"ORIGINAL: \" + w, ConsoleColor.Yellow)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 .Select(word =&gt; word.Trim())\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 .Dump(w =&gt; \"TRIMMED: \" + w, ConsoleColor.Yellow)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 .Select(word =&gt; word.ToLower())\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 .Dump(w =&gt; \"LOWERCASE: \" + w, ConsoleColor.Green)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 .Where(word =&gt; word.StartsWith(\"k\"))\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 .Dump(w =&gt; \"FILTERED to 'K': \" + w, ConsoleColor.Red)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 .OrderBy(word =&gt; word)\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0.Dump(w =&gt; \"SORTED: \" + w, ConsoleColor.Blue);\r\n}\r\n<\/pre>\n<p class=\"illustration\"><img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1194-Sorens8.jpg\" alt=\"1194-Sorens8.jpg\" \/><\/p>\n<p class=\"caption\">Figure 4 Output from Injecting the Dump Method<\/p>\n<p>The labeled\/color-coded output clearly communicates what step generates each line of output. It also reveals that LINQ really is a pipeline! Observe that the first word goes through the first 4 LINQ methods before the second word is even touched. The second word only survives the first 3 methods because it fails to make it through the filter looking for words starting with &#8220;k&#8221;. After all five words are processed by the first four steps, the remaining list-now just 2 words-is processed by the <b>OrderBy<\/b> method. <b>OrderBy<\/b> processes the whole list as a unit so it knows to wait for all the previous steps in the chain to complete. Notice that after <b>OrderBy<\/b> the data again flows in a pipeline from the final <b>Dump<\/b> call to the main program, which does a plain <b>Console.WriteLine<\/b>, because the blue <b>Dump<\/b> output is interleaved with the white standard output.<\/p>\n<p>This injection technique is more powerful than the simple, inline approach given earlier. You could achieve a similar result by setting a breakpoint inside the <b>Dump<\/b> method, then manually examining values in the debugger. But this technique is particularly useful if you want to see a stream of output from a running program rather than stop at a breakpoint. It is also handy because you can compile in your injections and get diagnostic output without having to run inside Visual Studio. Also, by modifying the <b>Dump<\/b> method you can change your destination from the console to a log file for further analysis and post-processing. Finally, I encourage you to review <a href=\"http:\/\/community.bartdesmet.net\/blogs\/bart\/archive\/2009\/04\/23\/linq-to-objects-debugging.aspx\">DeSmet&#8217;s blog entry<\/a> where he discusses further ways to extend the <b>Dump<\/b> \/ <b>Watch<\/b> method.<\/p>\n<h2 id=\"ninth\">Dump Method in Visual Studio: Points to Take Away<\/h2>\n<ul>\n<li>The <b>Dump<\/b> method is transparent to LINQ: its input passes through unchanged.<\/li>\n<li>You can instrument any step(s) in the LINQ chain you want to watch.<\/li>\n<li>You can observe the pipelining to debug interactions.<\/li>\n<li>You can dump simple values or complex objects because the <b>Dump<\/b> method lets you specify an arbitrary lambda expression.<\/li>\n<li>You can output derived values: for example, show not just each word but also its length-see the ProcessWordList4 method in the accompanying LinqMethodChaining project.<\/li>\n<li>Optional color coding and labeling let you clarify your output.<\/li>\n<li>To color code without labeling, use an identity lambda expression (x =&gt; x).<\/li>\n<li>To label without color coding, omit the color argument. (This uses the single DefaultColor, an exposed property.)<\/li>\n<\/ul>\n<h1 id=\"tenth\">LINQPad: Visualizing Your Data<\/h1>\n<p>The techniques presented thus far give you useful and flexible capabilities for examining simple data. But when you want to examine complex objects you need the power of Joseph Albahari&#8217;s <a href=\"http:\/\/www.linqpad.net\/\">LINQPad<\/a>. LINQPad is one of those rare applications that is elegant, powerful, and well-designed. As soon as you start using it you know it is &#8220;just right&#8221;. Remember back when you discovered the awesome power of Visual Studio; LINQPad is like that, too. (I have no affiliation with the application or its author. \ud83d\ude42 LINQPad is a sandbox\/IDE for .NET languages (C#, VB, SQL, F#) that lets you develop code without all the overhead of creating solutions and projects required in Visual Studio. I use it primarily for C# work though I have read some intriguing articles recently that some people use it to completely replace SQL Server Management Studio!<\/p>\n<p>In the C# arena, LINQPad appears as if it converts C# from a compiled language to an interpreted language. You can just type in an expression and press <b>Execute<\/b> (F5). Change the language selector from <b>C# Expression<\/b> to <b>C# Statements<\/b> if you want to put a bit more code on the page, or to <b>C# Program<\/b> for full class support. So you can define classes if you need them but if you just want to try out a few isolated statements you can do that in an instant.<\/p>\n<p>Besides the benefit of having a sandbox without the overhead, LINQPad includes powerful output visualization that is particularly useful with LINQ. (I guess that it was designed with this in mind-hence the name-but LINQPad should really be called something like .NET-Pad; it is not at all restricted to just LINQ.) The data visualization of LINQPad is outstanding, but learning how to use it takes exactly one sentence:<\/p>\n<div class=\"note\">\n<p>Append <b>.Dump( )<\/b> or <b>.Dump(&#8220;<i>your title string<\/i>&#8220;)<\/b> to the end of something you want to examine.<\/p>\n<\/div>\n<p>That is it. Period. Honest. The remainder of this article just shows you some tips on how to gain the most leverage from that method call.<\/p>\n<h2 id=\"eleventh\">Basic LINQPad<\/h2>\n<p>As an introduction, I start with an illustration of two examples, borrowed from my previous article <a href=\"http:\/\/www.simple-talk.com\/dotnet\/.net-framework\/using-three-flavors-of-linq-to-populate-a-treeview\/\">Using Three Flavors of LINQ to Populate a TreeView<\/a>.<\/p>\n<p>First the data:<\/p>\n<pre class=\"lang:c# theme:vs2012\">string[] names = { \"Burke\", \"Connor\", \"Frank\", \"Everett\",  \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \"Albert\", \"George\", \"Harris\", \"David\" };\r\n<\/pre>\n<p>Here is the most basic of LINQ queries; the output is a sorted list of elements, where each element is just an item from the names array. The result is fed to the <b>Dump<\/b> method and the output appears in Figure 5, left side:<\/p>\n<pre class=\"lang:c# theme:vs2012\">(from item in names orderby item select item).Dump();<\/pre>\n<p>The second example builds on this with <i>query continuation<\/i>. The output is, again, a list of elements, but here each element is a more complex structure, containing a length and a collection of zero or more names (Figure 5, right side):<\/p>\n<pre class=\"lang:c# theme:vs2012\">var query = from item in names  \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 orderby item\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 group item by item.Length into lengthGroups\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 orderby lengthGroups.Key descending\r\n\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0select lengthGroups;\r\nquery.Dump();\r\n<\/pre>\n<p>This query effectively creates a two-level structure. The <b>select<\/b> clause-the final clause in the LINQ query-defines the top level (the lengthGroups). Working backwards through the query, at the second level, the lengthGroup elements are sorted by the length of each collected group in descending order. This second level (defined by the <b>group&#8230;by<\/b>) fills each lengthGroup with individual names, and the names within each lengthGroup are sorted in ascending order (the default on the first <b>orderby<\/b> clause).<\/p>\n<p class=\"illustration\"><img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1194-Sorens9.jpg\" alt=\"1194-Sorens9.jpg\" \/><\/p>\n<p class=\"caption\">Figure 5 Output from LINQPad&#8217;s Dump Method<\/p>\n<div class=\"float-right\"><img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1194-Sorens10.jpg\" alt=\"1194-Sorens10.jpg\" \/><\/p>\n<p class=\"caption\">Figure 6: Successive LINQpad Output <br \/>\n from a method chain<\/p>\n<\/div>\n<h2 id=\"twelveth\">LINQPad with Method Chaining<\/h2>\n<p>Dumping the output of a query is certainly useful. But it becomes significantly better still if you can peek inside the LINQ chain, just as you saw earlier with the ProcessWordList2 and ProcessWordList3 methods. Recall that those used a custom <b>Dump<\/b> method in Visual Studio that was specifically designed as a pass-through method.<\/p>\n<p>I have not seen it documented anywhere, but I thought that the LINQPad <b>Dump<\/b> method must surely be as well-designed as that, too! Here is the bird example shown earlier tailored for LINQPad. Paste this code fragment into a LINQPad buffer, set the language to <b>C# Statements<\/b>, and execute it.<\/p>\n<pre class=\"lang:c# theme:vs2012\">string[] Words = new string[] {\"\u00a0\u00a0 KOOKABURRA\", \"Frogmouth\", \"kingfisher\u00a0\u00a0 \", \"loon\", \"merganser\"};\r\n\u00a0Words\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 .Dump(\"ORIGINAL:\")\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 .Select(word =&gt; word.Trim())\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 .Dump(\"TRIMMED:\")\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 .Select(word =&gt; word.ToLower())\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 .Dump(\"LOWERCASE:\")\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 .Where(word =&gt; word.StartsWith(\"k\"))\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 .Dump(\"FILTERED to 'K':\")\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 .OrderBy(word =&gt; word)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 .Dump(\"SORTED:\");\r\n<\/pre>\n<p>Figure 6 displays the result: a series of lists presented in a way that is instantly comprehensible. You see each step of the LINQ chain and can watch as each transformation occurs. The LINQPad <b>Dump<\/b> method is indeed transparent, returning its input unchanged to the next step in the chain!<\/p>\n<p>This <b>Dump<\/b> method has a different signature than the custom <b>Dump<\/b> method presented earlier for Visual Studio use. The earlier one had two signatures: one with no arguments and one with two arguments, an IEnumerable&lt;T&gt; and a ConsoleColor. This one also has two signatures: one with no arguments and one with a single string. For the latter, the string is used as a title on the list block that follows.<\/p>\n<p>Another difference to note is that this <b>Dump<\/b> method shows all the results from one step, then all the results from the next step, etc. The earlier <b>Dump<\/b> method showed individual results from one step intermingled with those of other steps, and let you see the actual sequence of execution. LINQPad is not changing the way the LINQ chain executes here; rather, I assume it is just collecting all the results internally and repackaging them in a clean visualization before presenting them.<\/p>\n<h2 id=\"thirteenth\">Examining Complex Objects<\/h2>\n<p>The <b>HostSwitcher subset.linq<\/b> file in the accompanying code archive contains an excerpt of the HostSwitcher code, including the <b>CreateMap<\/b> method shown near the beginning of this article. This real-world example lets you experiment with complex objects in LINQPad. Open the file in LINQPad and execute it and you get the dump of two structures (Figure 7).<\/p>\n<p class=\"illustration\"><img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1194-Sorens11.jpg\" alt=\"1194-Sorens11.jpg\" \/><\/p>\n<p class=\"caption\">Figure 7 LINQPad Inspection of HostSwitcher&#8217;s Dictionary and Context Menu<\/p>\n<p>The project dictionary displays the result of the <b>CreateMap<\/b> method that uses a complex LINQ chain to build a dictionary. Notice from the dump that it is a dictionary with string keys and IEnumerable&lt;ServerGroup&gt; values. The dictionary is available in a property so <b>Dump<\/b> can be directly invoked on that property, as shown in the first couple lines of the main program:<\/p>\n<pre class=\"lang:c# theme:vs2012\">void Main()  {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 HostManager hostManager = new HostManager();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 hostManager.CreateMap();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 hostManager.projectDict.Dump(\"Project Dictionary\");\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ContextMenuStrip contextMenuStrip = new ContextMenuStrip ();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 hostManager.BuildContextMenu(contextMenuStrip);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 hostManager.DumpContextMenu(contextMenuStrip);\r\n}\r\n<\/pre>\n<p>One of the uses of the dictionary is to create a dynamic context menu, enumerating each server group for each project. The context menu is constructed with other LINQ code so it is useful to check its structure as well. The context menu dump in Figure 7 displays the result of the <b>BuildContextMenu<\/b> method. <b>Dump<\/b> is also used here, but it is embedded in the <b>DumpContextMenu<\/b> method, which reformats the completed context menu before feeding it to <b>Dump<\/b> to get a more compact and meaningful output:<\/p>\n<pre class=\"lang:c# theme:vs2012\">public void DumpContextMenu(ContextMenuStrip contextMenuStrip)  {\r\n\u00a0 contextMenuStrip.Items\r\n\u00a0 .Cast&lt;ToolStripItem&gt;()\r\n\u00a0 .Select(item =&gt; new\r\n\u00a0\u00a0\u00a0\u00a0 {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Text = (item is ToolStripSeparator ? \"-separator-\" : item.Text),\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Items = (\r\n\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0item is ToolStripMenuItem ?\r\n\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0((ToolStripMenuItem) item).DropDownItems\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 .Cast&lt;ToolStripItem&gt;()\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0.Select(subitem =&gt; new { subitem.Text, subitem.ToolTipText } )\r\n\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0: null\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 )\r\n\u00a0\u00a0\u00a0\u00a0 })\r\n\u00a0 .Dump(\"Context Menu\");\r\n}\r\n<\/pre>\n<p>This method starts with the ToolStripItemCollection of the contextMenuStrip. Recall, however, that LINQ has great affinity for IEnumerable&lt;T&gt; objects. The <b>Cast<\/b> extension method converts the ToolStripItemCollection to the more palatable IEnumerable &lt;ToolStripItem&gt; for further processing. The Select method enumerates all the items in the context menu, with the label in the first output column and the contents in the second. The contents are generated by a nested LINQ query that extracts the label and the tooltip from each second-level menu item.<\/p>\n<p>Both of these dumps show how LINQPad gives a great visualization of your output. But applying what you now know about injecting a watcher into the chain, it is a trivial matter to examine the innards of the LINQ steps in the <b>CreateMap<\/b> method. I have included five <b>Dump<\/b> method calls in the <b>CreateMap<\/b> method, but they are all commented out. Here is the same <b>CreateMap<\/b> method shown earlier, now with the <b>Dump<\/b> method calls included:<\/p>\n<pre class=\"lang:c# theme:vs2012\">private void CreateMap()\r\n{\r\n\u00a0\u00a0\u00a0 projectDict = \r\n\u00a0\u00a0\u00a0 hostFileData\r\n\u00a0\u00a0\u00a0 .Select(line =&gt; new { line, m = FilteringRegex.Match(line) })\r\n\u00a0\u00a0\u00a0 \/\/.Dump()\r\n\u00a0\u00a0\u00a0 .Where(item =&gt; item.m.Success)\r\n\u00a0\u00a0\u00a0 \/\/.Dump()\r\n\u00a0\u00a0\u00a0 .Select(item =&gt; new\r\n\u00a0\u00a0\u00a0 {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 item.line,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 project = item.m.Groups[ProjectSubGroupIndex].ToString().Trim(),\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 serverGroup = item.m.Groups[ServerGroupSubGroupIndex].ToString().Trim()\r\n\u00a0\u00a0\u00a0 })\r\n\u00a0\u00a0\u00a0 \/\/.Dump()\r\n\u00a0\u00a0\u00a0 .GroupBy(item =&gt; new { item.project, item.serverGroup }, item =&gt; item.line)\r\n\u00a0\u00a0\u00a0 \/\/.Dump()\r\n\u00a0\u00a0\u00a0 .GroupBy(projAndServer =&gt; projAndServer.Key.project)\r\n\u00a0\u00a0\u00a0 \/\/.Dump()\r\n\u00a0\u00a0\u00a0 .ToDictionary(\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 project =&gt; project.Key,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 project =&gt; project.Select(item =&gt;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 new ServerGroup\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Name = item.Key.serverGroup,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0EnabledCount\u00a0 = item.Where(line =&gt; !IsCommented(line)).Count(),\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 DisabledCount = item.Where(line =&gt;\u00a0 IsCommented(line)).Count()\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 )\r\n\u00a0\u00a0\u00a0 );\r\n}\r\n<\/pre>\n<p>Uncomment any of those to see the intermediate data structures built on the way to coalescing into the compact dictionary result in Figure 7. Figure 8 shows the first portion of each of the five <b>Dump<\/b> calls in LINQPad. Compare each to the code above:<\/p>\n<ol>\n<li>After the first Select method, the data is projected into records with two fields. All inputs records are included because this point occurs before any filtering-notice the count of 26 records indicated at the top of the output.<\/li>\n<li>Here the output is filtered to include only those records with successful regular expression matches; the count is down to 16 records.<\/li>\n<li>The data is reformatted again to project into records with three fields that will be used in subsequent steps; there are still 16 records at this point.<\/li>\n<li>The first <b>GroupBy<\/b> reorganizes the data to 5 records grouped by project and server group.<\/li>\n<li>The second <b>GroupBy<\/b> then nests those groups in a parent grouping of just projects. This grouping arrangement then allows applying the <b>ToDictionary<\/b> method to get the final dictionary required.<\/li>\n<\/ol>\n<p class=\"illustration\"><img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1194-Sorens12.jpg\" alt=\"1194-Sorens12.jpg\" \/><\/p>\n<p class=\"caption\">Figure 8 LINQPad&#8217;s Inspection of HostSwitcher&#8217;s LINQ Chain<\/p>\n<h2 id=\"fourteenth\">There and Back Again<\/h2>\n<p>As I was developing HostSwitcher&#8217;s <b>CreateMap<\/b> method in Visual Studio, I lamented that I could not see the data structures the way LINQPad could show them to me. So I copied most of my code into a new LINQPad file, added the appropriate references, and then worked on the method in LINQPad, copying it back to Visual Studio when I completed it.<\/p>\n<p>Unfortunately, there is no automatic way to copy a Visual Studio project into LINQPad. I asked the author Joseph Albahari about importing a Visual Studio project into LINQPad in this <a href=\"http:\/\/stackoverflow.com\/questions\/4148601\/automatic-way-to-move-visual-studio-project-into-linqpad\">StackOverflow post<\/a>; while LINQPad does not do this, he is now thinking about at least adding a way to import references from a Visual Studio project. And, more immediately interesting, he pointed me to a Visual Studio add-in called <a href=\"http:\/\/code.google.com\/p\/linqpadvisualizer\/\">LINQPad Visualizer<\/a> by Robert Ivanc.<\/p>\n<p>With Ivanc&#8217;s visualizer, you can get LINQPad&#8217;s <b>Dump<\/b> output inside Visual Studio! To do this, you need to set up a watch expression while you are debugging your code. But before you do that you need to install the add-in to Visual Studio. This is a two-step process. Be aware, however, that at the time of writing LINQPad Visualizer does <i>not<\/i> support Visual Studio 2010 yet, though Ivanc has assured me it is on his &#8220;to do&#8221; list.<br \/>\n <em><strong>2010.12.04 <\/strong><\/em><strong>Breaking news:<\/strong> Just hours ago Robert Ivanc released a version that supports VS2010!<\/p>\n<ol>\n<li>From the link above, obtain the linqpadvisualizer.dll and, as Robert indicates in his instructions, copy the dll to the Visualizers folder of your Visual Studio instance (e.g. Documents\\Visual Studio 2008\\Visualizers). If the Visualizers folder does not exist, just create one.<\/li>\n<li>Copy the LINQPad executable (LINQPad.exe) into the same Visualizers folder. <i>Also<\/i> copy it to the same folder where Visual Studio&#8217;s devenv.exe executable resides (e.g. C:\\Program Files (x86)\\Microsoft Visual Studio 9.0\\Common7\\IDE). Another important point here: LINQPad is available for both .NET 3.5 and .NET 4.0. You can actually run both on your system without conflict. For LINQPad Visualizer, though, you must use the version for .NET 3.5 (which is LINQPad version 2.x ! ).<\/li>\n<\/ol>\n<p>Because of the restriction of LINQPad Visualizer to Visual Studio 2008, the accompanying code archive includes a VS2008 version of the HostSwitcher solution so you can try out the code as you read on.<\/p>\n<p>As shown in Figure 9, advance the debugger so that the object you are interested in is in scope (point 1)-notice the current line marked with the yellow arrow at the bottom of the code window. Next, open the watch window (point 2)-this should be one of the tabs in the group of tabs containing your errors, output, find, etc. Enter a new watch expression of this form:<\/p>\n<pre>new System.WeakReference(variable_to_inspect)<\/pre>\n<p>Upon pressing return in the Name column of the watch window, you should see the WeakReference show up in the Value column with-and this is the important point-a dropdown indicator on the right edge of the Value field (point 3). Open that dropdown and select (probably) the only visualizer available, the Linqpad (sic) Visualizer. Upon making that selection, you should get a new pop-up window showing the output of the variable you specified in the same form as LINQPad&#8217;s <b>Dump<\/b> method would render it (point 4). My example shows the dictionary created by the <b>CreateMap<\/b> method, exactly as you saw it in Figure 7.<\/p>\n<p class=\"illustration\"><img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1194-Sorens13.jpg\" alt=\"1194-Sorens13.jpg\" \/><\/p>\n<p class=\"caption\">Figure 9 Using LINQPad&#8217;s Inspection in Visual Studio<\/p>\n<p>LINQPad Visualizer definitely has value but there are a few issues to keep in mind:<\/p>\n<ol>\n<li>As already mentioned it does not yet provide Visual Studio 2010 support.<\/li>\n<li>If you leave the watch definition in place, the next time you debug the project and open the watch window the value field says &#8220;This expression causes side effects and will not be evaluated.&#8221; At the right edge instead of a dropdown icon you will find a refresh icon. Simply click that refresh icon to restore the dropdown.<\/li>\n<li>Most significantly, LINQPad Visualizer can only inspect objects that are marked as Serializable. (Ivanc clearly mentions this as a limitation on his web site, so kudos to him for that.) Unfortunately, I still had a bit of trouble with the dictionary example I have been using. If you look carefully in Figure 9 you will observe that the code for <b>CreateMap<\/b> is somewhat different than the code listing I originally presented for the method. To demonstrate LINQPad Visualizer I had to revert to this earlier version of the method. The more streamlined code (using the ToDictionary LINQ method) causes LINQPad Visualizer to throw an exception complaining that the new ServerGroup() construct is a non-serializable type even though it does have the [Serializable] attribute.\n<p> <em><strong>010.12.04<\/strong><\/em> <strong>Breaking news:<\/strong> Ivanc just identified what caused the exception I encountered! Technically it was a user error (mine) but you need to know this vital piece of information to avoid it: The catch is that you cannot serialize things that are lazy evaluated, so by forcing evaluation (with for example a ToList() call) you convert to something that can be serialized. So my final code for CreateMap&#8211;the version with ToDictionary&#8211;may be used by adding a ToList() to the ToDictionary code segment (I have replaced a chunk of code with an ellipsis for clarity) as shown:<br \/>\n <code>\u00a0\u00a0\u00a0 .ToDictionary(<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 project =&gt; project.Key,<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 project =&gt; project.Select(item =&gt; ...).ToList()<br \/>\n\u00a0\u00a0\u00a0 );<br \/>\n<\/code><\/li>\n<\/ol>\n<p>I point out the defects I have found not to condemn the product, but rather to help you work through them. I commend Ivanc on his efforts and look forward to improvements with this handy utility.<\/p>\n<h1 id=\"fifteenth\">Conclusion<\/h1>\n<p>LINQ is a tremendous productivity boost when you understand its capabilities. Fortunately, it is a technology that you can learn a bit at a time and also apply a bit at a time; it does not require nor demand wholesale conversion from previous techniques. Use the <b>Dump<\/b> method presented here to prevent your LINQ chains from becoming opaque as you delve into more and more complex chains. As you are learning LINQ, LINQPad is invaluable, letting you experiment with code fragments with ease. But it is not just a tool for learning; it is great for &#8220;real-world&#8221; code development in general. When you need to work out some data flow, copy pieces over to LINQPad so you can developer and\/or fine-tune it. Alternately, if it is cumbersome to find all the tendrils of the code you are working with to move it to LINQPad, bring LINQPad into Visual Studio with the handy LINQPad Visualizer, subject to the caveats mentioned. If you have not yet experienced LINQ, now is the time to give it a try!<\/p>\n<h1 id=\"sixteenth\">Footnotes<\/h1>\n<p>[1] The code archive accompanying this article includes: a VS 2010 solution (ChainingAndDebugging) illustrating dumping in Visual Studio; a VS 2008 solution (HostSwitcher2008) illustrating the LINQPad Visualizer; and a LINQPad Queries folder with LINQPad examples.<\/p>\n<p>[2] <i>Impedance matching<\/i> is a design practice in electrical engineering (used here as an analogy) whereby the output of one stage is designed to most efficiently and effectively match the input requirement of a subsequent stage in a pipeline process.<\/p>\n<p>[3] LINQ comes in several main flavors-LINQ to SQL, LINQ to XML, LINQ to DataSet, and LINQ to Objects-and a whole variety of lesser known ones, too. This article focuses on LINQ to Objects but the principles apply to LINQ in general.<\/p>\n<p>[4] See my earlier article <a href=\"http:\/\/www.simple-talk.com\/dotnet\/.net-framework\/using-linq-lambda-expressions-to-design-customizable-generic-components\/\">Using LINQ Lambda Expressions to Design Customizable Generic Components<\/a> for more on query syntax vs. method syntax.<\/p>\n<p>[5] For further reference on LINQ operators, see the MSDN reference pages <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb341635.aspx\">Enumerable Methods<\/a> and <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb397896%28v=VS.100%29.aspx\">Standard Query Operators Overview<\/a>.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>LINQ has the capabilities of providing a great productivity boost. LINQ Chaining is particularly  powerful magic, giving your code greater clarity and brevity. Using it, and debugging it, can be tricky without the right tools and techniques, but Michael is on hand to explain and make suggestions. <\/p>\n<p>&hellip;<\/p>\n","protected":false},"author":221868,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[143538],"tags":[4143,4229,4178,4706,5190],"coauthors":[6802],"class_list":["post-1044","post","type-post","status-publish","format-standard","hentry","category-dotnet-development","tag-net","tag-net-framework","tag-bi","tag-linq","tag-michael-sorens"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/1044","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/221868"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=1044"}],"version-history":[{"count":9,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/1044\/revisions"}],"predecessor-version":[{"id":75033,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/1044\/revisions\/75033"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=1044"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=1044"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=1044"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=1044"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}