Skip to content

Instantly share code, notes, and snippets.

@vendettamit
Created February 12, 2016 18:57
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save vendettamit/9ee03f693c41e2536433 to your computer and use it in GitHub Desktop.
A snippet to print nested IGrouping items from a GroupBy Linq.
public static class LinqVisualizer
{
public static void Dump<Tkey, Telement>(this IEnumerable<IGrouping<Tkey, Telement>> source)
{
foreach (var item in source)
{
Console.WriteLine("Root Level Key : {0}", item.Key);
foreach (var group in item)
{
Dump(group, true);
}
}
}
public static void Dump(dynamic obj, bool isFirstLevel)
{
if (obj.GetType().Name == "Grouping")
{
Dump(obj);
}
else
{
Console.WriteLine(" item - {0}", obj);
}
}
public static void Dump(dynamic source)
{
Console.WriteLine(" Key - {0}", source.GetType().GetProperty("Key").GetValue(source));
foreach (var item in source)
{
Dump(item, false);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment