Reputation: 34188
i am new in linq. so sometime just do not understand how linq is working. so there any tool or buil-in mechanism in VS2010 IDE to debug linq execution in detail. suppose i have this linq query
var to_search = new[] { "Geo", "JCB" };
var result = from sr in list
let w = to_search.FirstOrDefault(ts => sr.Title.ToLower().Contains(ts.ToLower()))
where w != null
let a = new {sr=sr, word=w.ToLower()}
group a by a.word into g
orderby g.Count() descending
let sorted = g.OrderByDescending(a=> a.sr.Title.Select((c, i) => a.sr.Title.Substring(i)).Count(sub => sub.ToLower().StartsWith(a.word)))
from a in sorted
select a.sr;
var completeList = result.Concat(list.Except(result));
how to debug this above linq query in detail. please guide me. thanks.
Upvotes: 4
Views: 6727
Reputation: 36688
Much can be written in response to this deceptively simple question! In fact, I have written at length in my article LINQ Secrets Revealed: Chaining and Debugging on Simple-Talk.com. Here is a summary of its key points:
IEnumerable<T>
as input and returns IEnumerable<T>
as output..Select(z => z)
in a method chain without consequence, use a variation of that, to wit: z => { return z; }
.Dump()
method. Dump is an object visualizer that provides amazing visualizations of complex data structures.Dump
back into Visual Studio--I provide code attached to my article mentioned at the top.Dump()
method).As a brief example, consider this simple method chain:
string[] Words = new string[]
{" KOOKABURRA", "Frogmouth", "kingfisher ", "loon", "merganser"};
Words
.Select(word => word.Trim())
.Select(word => word.ToLower())
.Where(word => word.StartsWith("k"))
.OrderBy(word => word);
Once you include the Dump extension method in your Visual Studio project, you can minimally instrument it like this...
Words
.Select(word => word.Trim())
.Dump()
.Select(word => word.ToLower())
.Dump()
.Where(word => word.StartsWith("k"))
.Dump()
.OrderBy(word => word)
.Dump();
... or more elaborately like this...
Words
.Dump(w => "ORIGINAL: " + w, ConsoleColor.Yellow)
.Select(word => word.Trim())
.Dump(w => "TRIMMED: " + w, ConsoleColor.Yellow)
.Select(word => word.ToLower())
.Dump(w => "LOWERCASE: " + w, ConsoleColor.Green)
.Where(word => word.StartsWith("k"))
.Dump(w => "FILTERED to 'K': " + w, ConsoleColor.Red)
.OrderBy(word => word)
.Dump(w => "SORTED: " + w, ConsoleColor.Blue);
... to get output rendered as either the left side or right side of the figure, respectively:
As a teaser, I will say that while this is indeed useful, you really must see the enhanced visualization that LINQPad can do with the same output (here is the link again for your convenience).
Upvotes: 8
Reputation: 46
The way I do is inserting breakpoints in the query, or use a tool called LINQPad. http://www.linqpad.net/
Upvotes: 1
Reputation: 67065
I believe that you should be able to place break points just as you normally would in any other code. I am not 100% sure as I often write the LINQ syntax out as the matching extension methods.
But, that is another option, you could rewrite the query using extension methods and add break points on each method.
When in doubt, though, use F11 :)
Also, LINQPad should be able to help a good bit here.
Upvotes: 0