I have an IList of type Breadcrumb which is just a lightweight class that has NavigationTitle, NavigationUrl and IsCurrent properties. It is cached on the webserver. I have a method that builds out the current breadcrumb trail up until the first Breadcrumb that has IsCurrent set to true… using the code below. Its very ugly and definitely a quick dirtbag willie solution, but I was curious, can this be easily refactored into LINQ?
IList<Breadcrumb> crumbs = new List<Breadcrumb>(); bool foundCurrent = false; for (int a = 0; a < cachedCrumbs.Count; a++) { crumbs.Add(crumbs[a]); if (foundCurrent) { break; } foundCurrent = (crumbs[a + 1] != null && ((Breadcrumb)crumbs[a + 1]).IsCurrent); }
I’m typing this as I think, so that it shows a train of thought as well as just an answer.
So, we end up with:
I haven’t tried this, but I think it should work… there might be a simpler way though.
EDIT: I’d argue that actually a straight foreach loop is simpler in this case. Having said that, you could write another extension method which acted like TakeWhile except it also returned the element which caused the condition to fail. Then it would be as simple as:
(I can’t think of a decent name for the method at the moment, hence
NewMethod!)