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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:38:53+00:00 2026-05-15T12:38:53+00:00

Given the following two classes: public class ABC { public void Accept(Ordering<User> xyz) {

  • 0

Given the following two classes:

public class ABC
{
    public void Accept(Ordering<User> xyz)
    {
        // Do stuff with xyz...
    }
}

public class Ordering<TEntity>
        where TEntity : class
{
    private readonly Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> Transform;

    private Ordering(Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> transform)
    {
        this.Transform = transform;
    }

    public static Ordering<TEntity> By<TKey>(Expression<Func<TEntity, TKey>> expression)
    {
        return new Ordering<TEntity>(query => query.OrderBy(expression));
    }

    public static Ordering<TEntity> ByDescending<TKey>(Expression<Func<TEntity, TKey>> expression)
    {
        return new Ordering<TEntity>(query => query.OrderByDescending(expression));
    }

    public Ordering<TEntity> ThenBy<TKey>(Expression<Func<TEntity, TKey>> expression)
    {
        return new Ordering<TEntity>(query => this.Transform(query).ThenBy(expression));
    }

    public Ordering<TEntity> ThenByDescending<TKey>(Expression<Func<TEntity, TKey>> expression)
    {
        return new Ordering<TEntity>(query => this.Transform(query).ThenByDescending(expression)); 
    }

    public IOrderedQueryable<TEntity> Apply(IQueryable<TEntity> query)
    {
        return Transform(query);
    }

}

Used in the following way:

ABC abc = new ABC();
abc.Accept(Ordering<User>.By(u => u.Id));

Is there any way to infer the type of T like so:

abc.Accept(Ordering.By(u => u.Id));
  • 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-15T12:38:54+00:00Added an answer on May 15, 2026 at 12:38 pm

    You can do it, but not in a generic type. Generic type inference like this only occurs for generic methods. Declare a separate non-generic type, with a generic method:

    public class XYZ
    {
        public static XYZ Action<T, TKey> (TKey key, T element)
        {
            return new XYZ<T>(element);
        }
    }
    

    EDIT: Responding to the question edit.

    No, you can’t do something like this:

    abc.Accept(Ordering.By(u => u.Id));
    

    The problem is the inner expression:

    Ordering.By(u => u.Id)
    

    What’s the type of u here? It could be any class with an Id property. Note that the C# compiler will need to work out the type of this expression before it looks at abc.Accept. Even if abc.Accept only worked for an Ordering<User>, it would fail.

    There are three options here:

    • Use a static method in the generic class, specifying the source type argument explicitly, and inferring the key type argument:

      Ordering<User>.By(u => u.Id)
      
    • Use a generic method in a non-generic class, specifying both type arguments explicitly:

      Ordering.By<User, string>(u => u.Id)
      
    • Use a generic method in a non-generic class, specifying the type of the lambda parameter explicitly, and letting the compiler infer the key type argument:

      Ordering.By((User u) => u.Id)
      

    Obviously, all of these cases require you to specify the type explicitly somewhere.

    One other option which is a little bit weird is relevant if you’ve typically already got an instance of User (or at least a variable of that type). You can use that as a sort of example, which gets ignored:

    public static Ordering<T> By<T,TKey>(Expression<Func<T, TKey>> func, T example)
    {
        return By<T, TKey>(func);
    }
    ...
    
    Ordering.By(u => u.Id, dummyUser);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 492k
  • Answers 492k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer in other words, has anything been done to the compiler… May 16, 2026 at 10:21 am
  • Editorial Team
    Editorial Team added an answer You would have to perform a topological sort on the… May 16, 2026 at 10:21 am
  • Editorial Team
    Editorial Team added an answer Python is completely different than C/C++, so it's hard to… May 16, 2026 at 10:21 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have created two partial classes for a web page. Now i have given
Let's say I have the following class structure: class Car; class FooCar : public
I have two classes which represent a special kind of numeric value. Let's call
I am getting the following exception. There are two records in the table and
Given the following snippet: #include <stdio.h> typedef signed long long int64; typedef signed int
Given the following InsertItemTemplate (simplified) I'm not getting anything back in the event object's
I've been testing out some of the new stuff in VS 2010 and C#
I'm thinking about table layout for a Hibernate-managed class hierarchy, and certainly the table
I'm trying to write two simple macros for begin and end of functions in
According to Firebug console, we have the following in JavaScript: >>> [''] == ''

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.