C # LINQ Debugging through IEnumerable Extension Method

I am trying to execute the following IEnumerable extension method, which I called DumpIt. The debugger will not go through it. How can I get the debugger to enter this extension method?

My unit test:

[Test] public void TestDumpIt() { var words = new string[] {" KOOKABURRA", "Frogmouth", "kingfisher ", "loon", "merganser"}; var result = words .DumpIt(d => "original: " + d) .Select(word => word.Trim()) .DumpIt(d => "trimmed: " + d) .Select(word => word.ToLower()) .DumpIt(d => "lower cased: " + d) .Where(word => word.StartsWith("k")) .DumpIt(d => "starts with k: " + d) .OrderBy(word => word) .DumpIt(d => "orderd: " + d); } 

Extension Method:

 public static IEnumerable<T> DumpIt<T>(this IEnumerable<T> input, Func<T, string> toString ) { foreach (var item in input) { Output.ActivePane.Activate(); Output.ActivePane.OutputString( ShowWhiteSpace ? '[' + toString(item) + ']' : toString(item)); yield return item; } } 

Thanks for any help!

+4
source share
1 answer

When using yield you are using deferred execution. Other LINQ methods that you use also use deferred execution. The DumpIt method will not actually be called until you start listing the result in TestDumpIt . If you want the code to be called, you need to list the enumerable. For example, you can simply add result.ToList() to the end for a forced enumeration.

+9
source

Source: https://habr.com/ru/post/1442987/


All Articles