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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:32:06+00:00 2026-05-13T00:32:06+00:00

I’m fond of defensive programming. I hate exception throwing, but this is not the

  • 0

I’m fond of defensive programming.
I hate exception throwing, but this is not the subject of my question.

I adapted an extension to linQ to be able to perform an order by with a column name

        public static IEnumerable<T> OrderBy<T>(this IEnumerable<T> list, string sortExpression)

With defensive programming, this method returns the given enumerable if the column name is not valid.

Now I need to perform a secondary sorting with ThenBy.
So I need that signature :

        public static IOrderedEnumerable<T> OrderBy<T>(this IEnumerable<T> list, string sortExpression)

I need to return a IOrderedEnumerable.
My problem is to keep my defensive programming feature : I must return the given set is the column name is invalid.

Is there a clean way to do this ? All I’m thinking of are kind of tricks :

  • Use reflection to order by the first found property, which is risky because the property may not be sorting-allowed
  • Implement my own IOrderedEnumerable, which is risky too because I perform ordering on IQueryable or IList, then I perform other LinQ operations, so I fear side effects.

And advice / suggestion ?
Thanks

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

    You may do any ordering. If column doesn’t exist, just leave your input enumerable as it was before. To do this, create key selector that returns the same value for all elements.

    See example:

    using System;
    using System.Linq;
    using System.Collections.Generic;
    using System.Reflection;
    
    static class Program
    {
        public static IOrderedEnumerable<T> OrderBy<T>(this IEnumerable<T> list, string sortExpression) where T : class
        {
            Func<T, Int32> keySelector = (elem) =>
            {
                PropertyInfo pi = typeof(T).GetProperty(sortExpression, typeof(Int32));
                if (pi == null)
                    return 0; // return the same key for all elements
    
                return Int32.Parse(pi.GetValue(elem, null).ToString());
            };
    
            return list.OrderBy(keySelector);
        }
    
        static void Main(string[] args)
        {
            // Create an array of strings to sort.
            string[] fruits = { "apricot", "orange", "banana", "mango", "apple", "grape", "strawberry" };
    
            // Sort by "column" Length
            foreach (string s in fruits.OrderBy<string>("Length"))
                Console.WriteLine(s);
            Console.WriteLine();
    
            // Sort by non-existing column
            foreach (string s in fruits.OrderBy<string>("ength"))
                Console.WriteLine(s);
            Console.ReadKey();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6
I need to clean up various Word 'smart' characters in user input, including but

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.