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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:16:38+00:00 2026-06-01T17:16:38+00:00

I have many collections for many types I want to sort each collection by

  • 0

I have many collections for many types I want to sort each collection by different properties. For example IEnumerable<Employee> will be sorted by Name and Age properties,and IEnumerable<Department> will be sorted by NumberOfEmployees and DepartmentName properties. I use PaginatedList to paginating the collection after sorting it.

public class PaginatedList<T> : List<T>
{
  public PaginatedList(IEnumerable<T> source, Int32 pageIndex, Int32 pageSize , Func<T,Object> orderBy)
  {
    this.AddRange(source.OrderBy(orderBy).Skip((PageIndex - 1) * PageSize).Take(PageSize));
  }
}

Note the 4th parameter which is the sorting delegate that will be passed to OrderBy extension method.

I am using a generic method to generate this 4th element

public Func<T, Object> SortingFactory<T>(String sortby) 
{
  switch (typeof(T).ToString())
  {
    case "Employee":
      switch(sortby)
      {
        case "Name":
           return new Func<Employee,String>(delegate(Employee e) { return e.Name; });
           break;                            
        case "Age":
           return new Func<Employee,Int32>(delegate(Employee e) { return e.Age; });
           break;
      }
      break;
    case "Department":
      switch(sortby)
      {
        case "NumberOfEmployees":
           return new Func<Department,Int32>(delegate(Department d) { return d.NumberOfEmployees; });
           break;                            
        case "DepartmentName":
           return new Func<Department,String>(delegate(Department d) { return d.DepartmentName; });
           break;
      }
      break;
  }
}

but it gives me a compilation ErrorCannot implicitly convert type 'System.Func<Employee,String>' to 'System.Func<T,object>'

I also tried to decalre the output as Func<Object,Object> but I got the same error.

What is the fault I made and how do such method.

  • 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-01T17:16:39+00:00Added an answer on June 1, 2026 at 5:16 pm

    Say I understood well

    public class PaginatedList<T> : List<T>
    {
      public PaginatedList(IEnumerable<T> source, Int32 pageIndex, Int32 pageSize )
      {
        this.AddRange(GetOrderFor<T>().Skip((PageIndex - 1) * PageSize).Take(PageSize));
      }
    }
    
    public static class Helpers
        {
    
            public static Func<T, object> GetSortExpression<T>(string sortExpressionStr)
            {
                var param = Expression.Parameter(typeof (T), "x");
                var sortExpression = Expression.Lambda<Func<T, object>>(Expression.Convert(Expression.Property(param, sortExpressionStr), typeof(object)), param);
                return sortExpression.Compile();
            }
    
            public static IOrderedEnumerable<T> GetOrderFor<T>(this IEnumerable<T> list)
            {
                switch (typeof (T).Name)
                {
                    case "Employee":
                        return list.OrderBy(GetSortExpression<T>("Name")).ThenBy(GetSortExpression<T>("Age"));
                    case "Category":
                        return list.OrderBy(GetSortExpression<T>("Name")).ThenBy(GetSortExpression <T> ("Id"));
                }
                return null;
            }
        }
    

    And if I misunderstood, I think the simple usage of GetSortExpression method can help you to avoid your error

    case "Employee":
          switch(sortby)
          {
            case "Name":
               return Helpers.GetSortExpression<T>("Name");                           
            case "Age":
               return Helpers.GetSortExpression<T>("Age");
          }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

EDIT1: I have tried many different strategies for deleting the element including this.model.destroy(), this.collection.remove(this.model),
I have many collections of documents in a mongo database that look like this:
I have a class with many collections loaded in memory. Is it possible to
I have a collection testcase like this, Test Builds have many Test Case Logs
Suppose I create collection like Collection<IMyType> coll; Then I have many implelentations of IMyTypem
I have heard it many times that garbage collection in PyS60 is not up
In many of our projects I have seen a few custom collection / or
I have an Entity which holds a collection of OtherEntity in a many-to-many style
I have many different NSArray 's stored in .dat files, in the Documents folder
I have a Question (q) with many Answers, each Answer has a variety of

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.