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 675913
In Process

The Archive Base Latest Questions

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

I have an ObservableCollection of Task objects. Each Task has the following properties: AssignedTo

  • 0

I have an ObservableCollection of Task objects. Each Task has the following properties:

  • AssignedTo
  • Category
  • Title
  • AssignedDate

I’m giving the user an interface to select which of these fields they was to sort by. In some cases, a user may want to sort by up to three of these properties. How do I dynamically build a LINQ statement that will allow me to sort by the selected fields in either ascending or descending order?

Currently, I’m trying the following, but it appears to only sort by the last sort applied:

var sortedTasks = from task in tasks
                  select task;

if (userWantsToSortByAssignedTo == true)
{
  if (sortByAssignedToDescending == true)
    sortedTasks = sortedTasks.OrderByDescending(t => t.AssignedTo);
  else
    sortedTasks = sortedTasks.OrderBy(t => t.AssignedTo);
}

if (userWantsToSortByCategory == true)
{
  if (sortByCategoryDescending == true)
    sortedTasks = sortedTasks.OrderByDescending(t => t.Category);
  else
    sortedTasks = sortedTasks.OrderBy(t => t.Category);    
}

Is there an elegant way to dynamicaly append order clauses to a LINQ statement?

  • 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-14T00:52:14+00:00Added an answer on May 14, 2026 at 12:52 am

    You can use ThenBy instead of OrderBy to apply more than one ordering. You will need an OrderBy(x => 1) at the beginning to start the process though.

    var sortedTasks = from task in tasks
                      orderby 1
                      select task;
    
    if (userWantsToSortByAssignedTo)
    {
        if (sortByAssignedToDescending)
            sortedTasks = sortedTasks.ThenByDescending(t => t.AssignedTo);
        else
            sortedTasks = sortedTasks.ThenBy(t => t.AssignedTo);
    }
    
    if (userWantsToSortByCategory)
    {
        if (sortByCategoryDescending)
            sortedTasks = sortedTasks.ThenByDescending(t => t.Category);
        else
            sortedTasks = sortedTasks.ThenBy(t => t.Category);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ObservableCollection of Patient objects. Each Patient object has a Name property,
I have an ObservableCollection<Person> object. The Person objects have Name and Type properties, where
I have this ListBox which is bound to an ObservableCollection. Each object in the
I have a listbox which has couple of items. When double clicked on each
I have ObservableCollection<ViewUnit> _myItems field, where ViewUnit implements INotifyPropertyChanged . ViewUnit has Handled :
I have a ObservableCollection<Class1> where Class1 contains x and y positions as properties. The
I have an ObservableCollection of about 1000 objects that needs to be filtered (searched)
I have a ObservableCollection<Object> that holds two different types of objects: Directory and File
I have an ObservableCollection of objects as follows: public class UserDataViewModel { private ObservableCollection<CategoryItem>
In my ViewModel I have this code: Logs = new ObservableCollection<Log>(); Logs = Task.Factory.StartNew(()

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.