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

  • Home
  • SEARCH
  • 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 5844113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:14:52+00:00 2026-05-22T12:14:52+00:00

I found Marc Gravell’s dynamic order by great: Dynamic LINQ OrderBy on IEnumerable<T> I’ve

  • 0

I found Marc Gravell’s dynamic order by great:

Dynamic LINQ OrderBy on IEnumerable<T>

I’ve put it in a class, LinqHelper. In this class I also have created two new classes, so that in my code I can do this:

var q = db.tblJobHeaders;

LinqHelper.OrderByCollection OBys = new LinqHelper.OrderByCollection();
OBys.AddOrderBy("some field", true);
OBys.AddOrderBy("anotherfield", false);
OBys.ExecuteOrderBys(q);

The classes to acheive this are:

/// <summary>
/// A collection of order bys
/// </summary>
public class OrderByCollection
{
    private ArrayList Orderings = new ArrayList();

    public OrderByCollection(){ }

    /// <summary>
    /// Add an order by to this collection
    /// </summary>
    public void AddOrderBy(string Field, bool Descending)
    {
        OrderByObj NewObj = new OrderByObj(Descending, Field);
        this.Orderings.Add(NewObj);
    }

    /// <summary>
    /// Executes the order bys
    /// </summary>
    public IOrderedQueryable<T> ExecuteOrderBys<T>(this IOrderedQueryable<T> source)
    {
        int ExecutionIndex = 0;
        foreach (OrderByObj O in this.Orderings)
        {
            if (ExecutionIndex == 0)
            {
                if (O.Descending)
                    source = LinqHelper.OrderByDescending(source, O.Field);
                else
                    source = LinqHelper.OrderBy(source, O.Field);
            }
            else
            {
                if (O.Descending)
                    source = LinqHelper.ThenByDescending(source, O.Field);
                else
                    source = LinqHelper.ThenBy(source, O.Field);
            }
            ExecutionIndex++;
        }
        return (IOrderedQueryable<T>)source;
    }
}

/// <summary>
/// An order by object
/// </summary>
private class OrderByObj
{
    public bool Descending { get; set; }
    public string Field { get; set; }

    public OrderByObj(bool IsDescending, string DatabaseField)
    {
        this.Descending = IsDescending;
        this.Field = DatabaseField;
    }
}

Howver I’m pretty new to passing Linq vars through to functions (the confuses me a bit). I currently get the error on:

OBys.ExecuteOrderBys(q);

Which gives the error:

The type arguments for method
‘LinqHelper.OrderByCollection.ExecuteOrderBys(System.Linq.IOrderedQueryable)’
cannot be inferred from the usage. Try
specifying the type arguments
explicitly.

I’m a bit confused about this if anyone could help, am I passing the var q in properly, and then returning it properly?

  • 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-05-22T12:14:52+00:00Added an answer on May 22, 2026 at 12:14 pm

    I bet the type of q is IQueryable<T> and not IOrderedQueryable<T>. Just changing the signature should work, because you start with OrderBy.

    Then you will need an IOrderedQueryable<T> for the ThenBys. You can just cast it, because you know for sure that you have an IOrderedQueryable<T> from the previous call to either OrderBy or ThenBy.

    If you don’t like the idea of the cast, you need some changes:

    public IOrderedQueryable<T> ExecuteOrderBys<T>(this IQueryable<T> source)
    {
        if(!this.Orderings.Any())
            throw new InvalidOperationException("You need to add orderings");
        IOrderedQueryable<T> ordered;
        if (this.Orderings[0].Descending)
            ordered = LinqHelper.OrderByDescending(source, this.Orderings[0].Field);
        else
            ordered = LinqHelper.OrderBy(source, this.Orderings[0].Field);
        foreach(var ordering in this.Orderings.Skip(1))
        {
            if (ordering.Descending)
                ordered = LinqHelper.ThenByDescending(source, ordering.Field);
            else
                ordered = LinqHelper.ThenBy(source, ordering.Field);
        }
        return ordered;
    }
    

    Note your code will fail spectacularly if you don’t add any orderings, because of the cast to IOrderedQueryable<T> in the end. You could change the return type to IQueryable<T> (which loses the ability to “attach” more OrderBys later), or throw if there are no orderings, like I did.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

@Marc Gravell gave a great example of how to install a Windows Service here
Found this great article on using jquery for image swapping: http://jquery-howto.blogspot.com/2009/05/replacing-images-at-time-intervals.html How do you
In this thread , Marc Bernier mentioned SWIG also generated a bunch of C#
found this on a blog : (def x ^{:type ::my-class} {}) apparently it adds
FOUND THAT CStarRating is causing this error if it is removed it gets ok.Also
Found this: Sub SurroundWithAppendTag() DTE.ActiveDocument.Selection.Text = .Append( + DTE.ActiveDocument.Selection.Text + ) End Sub But
found this regex: insert every 10 characters: $text = preg_replace(|(.{10})|u, \${1}. , $text); can
Found this rather strange bug in IE8; element.style.top is limited to 1342177 pixels. Even
Found this is some example CSS I was reading and I'm having a hard
Found this question that explained a way to communicate between layers. However there is

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.