Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8186451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:12:12+00:00 2026-06-07T02:12:12+00:00

Using Entity Framework 4, I’m trying to implement dynamic sorting based on a collection

  • 0

Using Entity Framework 4, I’m trying to implement dynamic sorting based on a collection of member names. Basically, the user can select fields to sort and the order of the sorting. I’ve looked at expression tree examples and can’t piece this together. Here are some details:

Collection of column names:

public List<string> sortColumns;
sortColumns = new List<string>();

/// Example subset of video fields.  The collection will vary.
sortColumns.Add("Width");
sortColumns.Add("Height");
sortColumns.Add("Duration");
sortColumns.Add("Title");

The video class is defined as follows:

public class Video
{
    public string Title { get; set; }
    public int Width { get; set; }
    public int Height { get; set; }
    public float Duration { get; set; }
    public string Filename { get; set; }
    public DateTime DateCreated { get; set; }
    .
    .
    .
}
public List<Video> Videos;

What I would like to do is enumerate through the sortColumns collection to build an expression tree at run time. Also, the user could specify either ascending or descending sorting and the expression tree should handle either.

I tried the Dynamic LINQ library for VS 2008, but it doesn’t appear to work in VS 2010. (I could be doing something wrong.)

The bottom line is I need an expression tree to dynamically sort the Videos collection based on user input. Any help would be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T02:12:14+00:00Added an answer on June 7, 2026 at 2:12 am

    First you need the OrderBy extension method that @Slace wrote here. All credit to Slace for an awesome piece of code and by far the most difficult part of the solution! I made a slight modification for it to work with your specific situation:

    public static class QueryableExtensions
    {
        public static IQueryable<T> OrderBy<T>(this IQueryable<T> source, string sortProperty, ListSortDirection sortOrder)
        {
            var type = typeof(T);
            var property = type.GetProperty(sortProperty);
            var parameter = Expression.Parameter(type, "p");
            var propertyAccess = Expression.MakeMemberAccess(parameter, property);
            var orderByExp = Expression.Lambda(propertyAccess, parameter);
            var typeArguments = new Type[] { type, property.PropertyType };
            var methodName = sortOrder == ListSortDirection.Ascending ? "OrderBy" : "OrderByDescending";
            var resultExp = Expression.Call(typeof(Queryable), methodName, typeArguments, source.Expression, Expression.Quote(orderByExp));
    
            return source.Provider.CreateQuery<T>(resultExp);
        }
    }
    

    Create a method to sort the list. A couple of things to note in the method below:

    1. The List<string> is converted to an IQueryable<string> since Enumerable operators don’t take expression trees.
    2. The method iterates through the list of sort columns in reverse order (assuming you want to give the first item in the list the highest sort priority).
    private void PrintVideoList(IEnumerable<string> sortColumns, ListSortDirection sortOrder)
    {
        var videos = this.GetVideos();
        var sortedVideos = videos.AsQueryable();
    
        foreach (var sortColumn in sortColumns.Reverse())
        {
            sortedVideos = sortedVideos.OrderBy(sortColumn, sortOrder);
        }
    
        // Test the results
        foreach (var video in sortedVideos)
        {
            Console.WriteLine(video.Title);
        }
    }
    

    You should then be able to use the method like this:

    // These values are entered by the user
    var sortColumns = new List<string> { "Width", "Title", "Height" };
    var sortOrder = ListSortDirection.Ascending;
    
    // Print the video list base on the user selection
    this.PrintVideoList(sortColumns, sortOrder);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Entity Framework, I am writing a social networking app and trying to setup
Im using entity framework 1.0 and trying to feed out a Gridview with a
I am using Entity Framework 4.3.1 and I am trying to insert a new
Using Entity Framework CTP 5 I am trying to make a list of foreign
I'm using Entity Framework 4 and a Dynamic Data site to expose a bare-bones
I am using Entity Framework. I am trying to get the results from linq
Using Entity Framework I can create concrete classes from most of the sprocs in
I am using Entity Framework in an MVC website I am trying to get
Using Entity Framework, I suddenly get this strange error after publishing my asp.net mvc
(USING ENTITY FRAMEWORK 4.3 CODE FIRST) The code is below. I want to actually

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.