You can specify the "OrderBy" criteria dynamically with the IdeaBlade.Linq.SortSelector when you can't determine the sort order at compile time.
You create an instance of the SortSelector class by specifying the type of object to sort, the property to sort, and its sort direction. The OrderBySelector extension method makes it easy to use a SortSelector in a standard LINQ query you would otherwise have used OrderBy, OrderByDescending, ThenBy or ThenByDescending. For example:
C# | var sort = new SortSelector(typeof(Customer), "Country", ListSortDirection.Descending); var customers = myEntityManager.Customers.OrderBySelector(sort).ToList(); // compare with the strongly-typed LINQ equivalent var customers = myEntityManager.Customers.OrderByDescending(c => c.Country).ToList(); |
VB | Dim sort = New SortSelector(GetType(Customer), "Country", ListSortDirection.Descending) Dim customers = myEntityManager.Customers.OrderBySelector(sort).ToList() ' compare with the strongly-typed LINQ equivalent Dim customers = myEntityManager.Customers.OrderByDescending(Function(c) c.Country).ToList() |
A single SortSelector instance can specify multiple sort properties, each with its own sort direction. You add sort criteria with the ThenBy method.
C# | var sort = new SortSelector(typeof(Customer), "Country", ListSortDirection.Descending); sort = sort.ThenBy("CompanyName"); // by default ListSortDirection is Ascending if not specified. var customers = myEntityManager.Customers.OrderBySelector(sort).ToList(); // compare with the strongly-typed LINQ equivalent var customers = myEntityManager.Customers .OrderByDescending(c => c.Country) .ThenBy(c => c.CompanyName) .ToList(); |
VB | Dim sort = New SortSelector(GetType(Customer), "Country", ListSortDirection.Descending) sort = sort.ThenBy("CompanyName") ' by default ListSortDirection is Ascending if not specified. Dim customers = myEntityManager.Customers.OrderBySelector(sort).ToList() ' compare with the strongly-typed LINQ equivalent Dim customers = myEntityManager.Customers .OrderByDescending(Function(c) c.Country) .ThenBy(Function(c) c.CompanyName) .ToList() |
SortSelectors can also be combined with an overload of the ThenBy method or with the static Combine method.
C# | // create two SortSelectors var sort1 = new SortSelector(typeof(Customer), "Country", ListSortDirection.Descending); var sort2 = new SortSelector(typeof(Customer), "CompanyName"); // combine them using either of the following two mechanisms. var sort = sort1.ThenBy(sort2); // or var sort = SortSelector.Combine(new[] { sort1, sort2 }); |
VB | ' create two PropertySelectors Dim sort1 = New SortSelector(GetType(Customer), _ "Country", ListSortDirection.Descending) Dim sort2 = New SortSelector(GetType(Customer), "CompanyName") ' combine them using either of the following two mechanisms. Dim sort = sort1.ThenBy(sort2) ' or Dim sort = SortSelector.Combine( { sort1, sort2 }) |
C# | // Type not specified; it is determined when used var sort = new SortSelector("Country", ListSortDirection.Descending); var customers = myEntityManager.Customers.OrderBySelector(sort).ToList(); |
VB | ' Type not specified; it is determined when used Dim sort = New SortSelector("Country", ListSortDirection.Descending) Dim customers = myEntityManager.Customers.OrderBySelector(sort).ToList() |
Such lazy-typing can be useful when sorting anonymous types.
The IdeaBlade.Linq.PropertySortSelector was the less-capable class for ordering query result dynamically in versions of DevForce prior to v.6.1.0. That class has been deprecated in favor of SortSelector and will be removed from the product at a future date.