Using Linq with ItemsCollection

I was working with an ItemCollection that was defining a menu. To hone in on a specific MenuItem I did something like this:
But I didn’t like. It just seemed big and old school. I wanted to use the leverage of Linq to simplify it. But I noticed quickly that I didn’t have access to the Linq helper functions. This is because ItemsCollection is only an IEnumerable, not a Typed IEnumerable. You can foreach it just fine but you have to use the Cast function to make it "Linq-ready". Here is my solution:
I had to first cast to an object because some items in the list were not MenuItem (e.g., MenuSeperator). Still not as clean as I’d like but a bit better. If I didn’t have seperators it could have been even simpler.

Read More

Conditional Ordering and Subsorting in LINQ

To accomplish conditional ordering you build upon your original LINQ assignment variable (in this case recs). If there are any records that haven’t been viewed by a user, the set is sorted one way, otherwise another.
As for subsorting notice the ThenByDescending method. I first sort ascending by a boolean that pushes all the false values to the top. I then subsort descendingly by a date. This will isolate the false group and subsort that group by a descending date. It will do the same with the true group.

Read More