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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:47:48+00:00 2026-05-22T01:47:48+00:00

I want to call a function that generates a function, but I don’t know

  • 0

I want to call a function that generates a function, but I don’t know the type parameter until I call the generated function like so:

ActionResult Foo() {
  IEnumerable<MyObject> list = GetList();

  var orderBy = OrderBy(list); // <-- HOW DO I WRITE OR DO THIS;

  switch(Request.QueryString["sortBy"]) {
    case "Name":
      return orderBy<string>(o => o.Name); // <-- SO I CAN MAKE THIS CALL
    case "TrackingNumber":
      return orderBy<int>(o => o.TrackingNumber); // <-- AND THIS ONE
    default:
      return View(list);
  }
}

I won’t know the type of T until I call the returned function. I imagine something like this to generate the function, it wraps list in a closure so that I don’t have to keep passing it around.

Func<Func<MyObject,T>,ActionResult> OrderBy<T>(IEnumerable<MyObject> list) {
  Func<Func<MyObject,T>, ActionResult> f = orderBy => {
     return View(Request.QueryString["sortDir"] == "d"
        ? list.OrderBy<MyObject, T>(orderBy)
        : list.OrderByDescending<MyObject, T>(orderBy));
  };
  return f;
}

Update:

I know there are better ways to do this. I want to know how I can wrap this list in a closure and return a function that has a generic type I don’t know about ahead of time. For example, since I can’t return the generic type function without knowing the type, is there some type I can replace it with?

  • 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-22T01:47:49+00:00Added an answer on May 22, 2026 at 1:47 am

    The trick is to identify the type that you care about that both types have in common. In this case, it’s IComparable. You’re sorting, and both strings and ints are IComparable.

    You’re also making the problem a lot more complicated than it needs to be. You don’t need so many Func‘s that return Func‘s. You can simplify.

    Also you don’t want ASP.NET MVC classes to permeate your entire application. Keep that stuff in the controllers and let the code that does the real work take parameters so it doesn’t know it’s running in ASP.NET MVC or in the web and can be unit tested.

        public static IEnumerable<MyObject> GetSortedList(string sortBy, bool sortAscending)
        {
            IEnumerable<MyObject> list = GetList();
    
            Func<MyObject, IComparable> keySelector;
    
            switch (sortBy)
            {
                case "Name":
                    keySelector = o => o.Name;
                    break;
    
                case "TrackingNumber":
                    keySelector = o => o.TracingNumber;
                    break;
    
                default:
                    return list;
            }
    
            return sortAscending
                       ? list.OrderBy(keySelector)
                       : list.OrderByDescending(keySelector);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple function that I want to call in the code behind
I want to call a web service, but I won't know the url till
I want to call a function from a .NET DLL (coded in C#) from
I want to call a function (which calculates my version number) when my NSIS
I want to call a c# function from my javascript function. I have a
I want to use LabVIEW's Call Library Function Node to access a DLL function,
I often want to make a multiline function call and reduce it down to
I want to make a link call a Javascript function through the onclick event
I want to call ShowDialog() when a keyboard hook event is triggered, but I'm
I've seen a few questions like this around SO but couldn't find anything that's

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.