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

Related Questions

Consider the following two Java classes: a.) class Test { void foo(Object foobar) {
I have the following registration: container.Register(AllTypes.FromAssemblyContaining<ITabViewModel>().BasedOn<ITabViewModel>()); Two classes: public class StructureDecorationViewModel : NotificationObject, ITabViewModel
Given the following code without considering friendship between two classes: class OutSideClass { ...
Given the following two classes: class Location < ActiveRecord::Base belongs_to :holiday_schedule validates :name, :presence
I have two classes declared as below: class User { public: MyMessageBox dataMsgBox; };
Given the following two ways of defining a class method in Ruby: class Foo
Given the following strings 7;#User One 7;#User Two;#9;#User Two 7;#User Two;#9;#User Two;#123;#User Three I
Given I have two File objects I can think of the following implementation: public
Lets say I have the following code: public class Base { // Some stuff
I have the following classes: public class ComplexType { public long? Code { get;

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.