Tuesday, May 31, 2011

Take me back!

After iterating the tree down, its time do go the opposite way: up!


public static IEnumerable<T> ToRoot<T>(
T item, Func<T, T> selector)
{
if (Equals(item, default(T))) yield break;
yield return item;

foreach (var x in ToRoot(selector(item), selector))
yield return x;
}


And how to use it:

 
static IEnumerable<int> CreateList(ProductDataSet.ProductRow row)
{
return ToRoot(row, x => x.ProductRowParent).Select(x => x.ProductId);
}

No comments:

Post a Comment