tags:

views:

50

answers:

1

I have a GridItemCollection defined as

GridItemCollection items = (GridItemCollection) Session["driveLayout"];

I want to sort the collection based off of one of the items in each GridItem. In particularly this item

item["VolumeGroup"].ToString().ToLower()

What is the best way to do this? Thanks

A: 

You can use the LINQ OrderBy extension method:

GridItemCollection items = (GridItemCollection)Session["driveLayout"];
var sortedItems = items.OfType<GridItem>().OrderBy(item => item.GridItems["VolumeGroup"].ToString().ToLower());

OfType() Converts an IEnumerable to an IEnumerable where T is the type specified in the type parameter. Since we know implicitly that GridItemCollection is a collection of items typed GridItem, we can do this.

Once we have an IEnumerable, we have access to all the LINQ extension methods, including OrderBy, which takes a lambda to use as the sort parameter.

Rex M