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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:52:16+00:00 2026-05-13T21:52:16+00:00

After searching online and going through past stackoverflow posts for a suitable implementation for

  • 0

After searching online and going through past stackoverflow posts for a suitable implementation for dynamic ordering with linq, i came up with my own implementation that borrows a few things from other solutions i have previously seen.

What i need to know is if this implementation is threadsafe? I don’t believe it is as i am passing an enumerable generic type object (reference type) as a parameter into the static method but i would like to know if i am missing anything else and hopefully how to make it completely threadsafe.

public class OrderByHelper
{

    public static IEnumerable<T> OrderBy<T>(IQueryable<T> items, string sortColumn, string sortDirection, int pageNumber, int pageSize)
    {
        Type t = typeof(T).GetProperty(sortColumn).PropertyType;
        return (IEnumerable<T>)(typeof(OrderByHelper)
                        .GetMethod("OrderByKnownType")
                        .MakeGenericMethod(new[] { typeof(T), t })
                        .Invoke(null, new object[] { items, sortColumn, sortDirection, pageNumber, pageSize }));
    }

    public static IEnumerable<K> OrderByKnownType<K, T>(IQueryable<K> items, string sortColumn, string sortDirection, int pageNumber, int pageSize)
    {
        var param = Expression.Parameter(typeof(K), "i");
        var mySortExpression = Expression.Lambda<Func<K, T>>(Expression.Property(param, sortColumn), param);

        if (!string.IsNullOrEmpty(sortDirection))
        {
            if (sortDirection == "ASC")
                return items.OrderBy(mySortExpression).Skip((pageNumber - 1) * pageSize).Take(pageSize);
            else
                return items.OrderByDescending(mySortExpression).Skip((pageNumber - 1) * pageSize).Take(pageSize);
        }
        else
            throw new InvalidOperationException("No sorting direction specified.");
    }
}
  • 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-13T21:52:17+00:00Added an answer on May 13, 2026 at 9:52 pm

    The answer is simple:

    It is thread safe if your original collection is thread safe.

    All extension methods in LINQ merely call .GetEnumerator() of the original collection. Sorting and Ordering doesn’t manipulate the original collection, but rather lets you enumerate it in a sorted order. Thus, you only do read operations on the data. As a general rule of thumb, if you only do read data, you do not need to implement any thread safety.

    I’m tempted to say that in 99% of cases you don’t need any thread safety, because you collect data only once and then expose the LINQ functionality. You might only need a thread safe collection if you want to create a framework that does not instantiate new collections when refreshing data, but rather keeps re-using the same (observable) collection instance that synchronizes itself with the database data in a fancy way.

    But I don’t know about your exact scenario. If you really do need thread safety, then it depends if you have control over the code where the original collection is instantiated and the data is added.

    If you are using merely LINQ-to-objects, then it’s entirely in your control to create an instance of a thread safe collection class at that spot.

    If you’re using LINQ-to-SQL or anything, then it might be difficult because the original collection that collects the data from the database is probably instantiated deep within the provider and hidden from you. I haven’t looked at it though if there are extension points where you can override stuff to use a thread safe collection instead.

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

Sidebar

Related Questions

After searching online and reading the Oracle documentation for this, I need some help
After searching through some existing libraries for JSON, I have finally ended up with
After searching stackoverflow.com I found several questions asking how to remove duplicates, but none
I recently discovered the shorthand if statement and after searching online I couldn't find
After searching online this is a question I have been unable to find an
After searching through SO, and not finding anything useful, I decided to post this
After searching online, the best solution I've found so far is to just make
After searching online for quite some time, I cannot find a good way to
After searching I dd not find anything about glassfish3.1 and CORS Is it possible
After searching in the Net for about 4 hours I still don't understand Async

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.