Print Page | Close Window

Silverlight: Dynamic OrderBy

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=2001
Printed Date: 21-Apr-2026 at 8:41pm


Topic: Silverlight: Dynamic OrderBy
Posted By: jkowalski
Subject: Silverlight: Dynamic OrderBy
Date Posted: 23-Jul-2010 at 1:04pm

There has got to be a better way to dynamically order a Silverlight Blade query.

 

Please advise.

 
 
var accounts = (IEntityQuery<Account>)accountQuery;

var orderedQuery = accounts.OrderBy(a => a.AccountName);

 

if(DefaultSortDirection == ListSortDirection.Ascending)

{

    switch (DefaultSortColumn.ToLower())

    {

        case "accountname":

            orderedQuery = accounts.OrderBy(a => a.AccountName);

            break;

        case "accountid":

            orderedQuery = accounts.OrderBy(a => a.AccountID);

            break;

        case "accountsubtype":

            orderedQuery = accounts.OrderBy(a => a.AccountSubtype);

            break;

        case "accounttype":

            orderedQuery = accounts.OrderBy(a => a.AccountType);

            break;

        case "address1":

            orderedQuery = accounts.OrderBy(a => a.Address1);

            break;

        }

}

else

{

    switch (DefaultSortColumn.ToLower())

    {

    case "accountname":

        orderedQuery = accounts.OrderByDescending(a => a.AccountName);

        break;

    case "accountid":

        orderedQuery = accounts.OrderByDescending(a => a.AccountID);

        break;

    case "accountsubtype":

        orderedQuery = accounts.OrderByDescending(a => a.AccountSubtype);

        break;

    case "accounttype":

        orderedQuery = accounts.OrderByDescending(a => a.AccountType);

        break;

    case "address1":

        orderedQuery = accounts.OrderByDescending(a => a.Address1);

        break;

    }

}

 

return orderedQuery;

 

 

 




Replies:
Posted By: GregD
Date Posted: 23-Jul-2010 at 3:45pm
You might find something here:

http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx%20 - LINQ Dynamic Query Library


Posted By: jkowalski
Date Posted: 23-Jul-2010 at 3:59pm
Sorry but the link is no good.


Posted By: jkowalski
Date Posted: 23-Jul-2010 at 4:31pm
found it.
 
Will this work with Silverlight?


Posted By: jkowalski
Date Posted: 26-Jul-2010 at 12:34pm
FYI: This does not work with Silverlight


Posted By: kimj
Date Posted: 27-Jul-2010 at 8:46am
Actually, some of it does.  The ClassFactory does not work in Silverlight, along with the CreateClass methods on DynamicExpression, but if you remove or comment all that out you can get the DynamicLibrary working.   I only tested the dynamic OrderBy so don't know what else might fail at run time, but this library is pretty handy.


Posted By: jkowalski
Date Posted: 29-Jul-2010 at 9:24am
Do you have the code that you test with?
 
Thanks for the info. I'll give it a try.


Posted By: kimj
Date Posted: 29-Jul-2010 at 10:43am
Well, I used the term "test" quite loosely - I ran a single query and it worked.  Here's the query -
     var customersQuery = _em1.Customers
        .Where(c => c.ContactTitle == "Sales Representative")
        .OrderBy("CompanyName")
        as EntityQuery<Customer>;
   
      _em1.ExecuteQueryAsync<Customer>(customersQuery, GotCustomers, null);
 
The changes to the DynamicLibrary were hacks, just to see if the code would compile.  I'll post it here, but I want to emphasize this is neither tested nor endorsed by IdeaBlade, and certainly not supported - it's just my hacking to try the DL in Silverlight.
 
http://www.ideablade.com/forum/uploads/11/DynamicLibrary.cs.txt - uploads/11/DynamicLibrary.cs.txt



Print Page | Close Window