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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:51:42+00:00 2026-06-15T07:51:42+00:00

I have a sorting extension method with the following signature: public static IEnumerable<T> CustomSort<T>(this

  • 0

I have a sorting extension method with the following signature:

public static IEnumerable<T> CustomSort<T>(this IEnumerable<T> source, string sortProperties)

We wrote it a while back and it has been doing its thing. Now I am creating a custom control and the DataSource property is an IEnumerable (non-generic).
Is there any way to get the type of the objects in a non-generic IEnumerable?

I am sure the problem of “sort a custom control data source” has been solved a million times, but I just can’t seem to find a solution.

  • 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-15T07:51:43+00:00Added an answer on June 15, 2026 at 7:51 am

    You could create an extension method that will return the correct type at runtime:

    public static class LinqExtensions
    {
        public static Type GetElementType(this IEnumerable source)
        {
            var enumerableType = source.GetType();
            if (enumerableType.IsArray)
            {
                return enumerableType.GetElementType();
            }
            if (enumerableType.IsGenericType)
            {
                return enumerableType.GetGenericArguments().First();
            }
            return null;
        }
    }
    

    Update: I’ve added the mechanism that I would use to perform the generic-specific IEnumerable<T> sorting on the non-generic IEnumerable

    public static class SortingExtensions
    {
        public static IEnumerable<T> CustomSort<T>(this IEnumerable<T> source, string sortProperties)
        {
            // sort here
        }
    
        public static IEnumerable CustomSort(this IEnumerable source, string sortProperties)
        {
            var elementType = source.GetElementType();
            var genericElementType = typeof (IEnumerable<>).MakeGenericType(elementType);
    
            var sortMethod = typeof (SortingExtensions).GetMethod(
                "CustomSort", 
                BindingFlags.Public | BindingFlags.Static,
                null, 
                new [] {genericElementType, typeof (string)},
                null);
    
            return (IEnumerable) sortMethod.Invoke(null, new object[] {source, sortProperties});
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have found an extension method that handles sorting and paging for LINQ .
I wrote an extension method to try and count the words in a string
I have the following html code with Jquery Sorting <div class=sortable style='width:700px; margin: 0
i have problem sorting the arrays.i have two problems. 1.Sorting rightArray with the following
I am writing a list sorting extension method. My input is the list and
The TakeWhile extension method has the following comment in its tooltip: the element's index
I have this app https://github.com/staticdev/django-crud-utils that need to have django-sorting installed to work. But
I have an array sorting function as follows: public function sortAscending($accounts) { function ascending($accountA,
I have the following code: public class OMyObject { public int Id { get;
I have a strange sorting issue with my models. Given the following: I have

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.