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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:08:12+00:00 2026-05-11T09:08:12+00:00

I’m trying to combine the following expressions into a single expression: item => item.sub,

  • 0

I’m trying to combine the following expressions into a single expression: item => item.sub, sub => sub.key to become item => item.sub.key. I need to do this so I can create an OrderBy method which takes the item selector separately to the key selector. This can be accomplished using one of the overloads on OrderBy and providing an IComparer<T>, but it won’t translate to SQL.

Following is a method signature to further clarify what I am trying to achive, along with an implementation that doesn’t work, but should illustrate the point.

    public static IOrderedQueryable<TEntity> OrderBy<TEntity, TSubEntity, TKey>(         this IQueryable<TEntity> source,          Expression<Func<TEntity, TSubEntity>> selectItem,          Expression<Func<TSubEntity, TKey>> selectKey)         where TEntity : class         where TSubEntity : class      {         var parameterItem = Expression.Parameter(typeof(TEntity), 'item');         ...         some magic         ...         var selector = Expression.Lambda(magic, parameterItem);         return (IOrderedQueryable<TEntity>)source.Provider.CreateQuery(             Expression.Call(typeof(Queryable), 'OrderBy', new Type[] { source.ElementType, selector.Body.Type },                  source.Expression, selector                  ));     }  

which would be called as:

.OrderBy(item => item.Sub, sub => sub.Key) 

Is this possible? Is there a better way? The reason I want an OrderBy method that works this way is to support a complex key selection expression that applies to many entities, though they are exposed in different ways. Also, I’m aware of a way to do this using String representations of deep properties, but I’m trying to keep it strongly typed.

  • 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. 2026-05-11T09:08:12+00:00Added an answer on May 11, 2026 at 9:08 am

    Since this is LINQ-to-SQL, you can usually use Expression.Invoke to bring a sub-expression into play. I’ll see if I can come up with an example (update: done). Note, however, that EF doesn’t support this – you’d need to rebuild the expression from scratch. I have some code to do this, but it is quite lengthy…

    The expression code (using Invoke) is quite simple:

    var param = Expression.Parameter(typeof(TEntity), 'item'); var item = Expression.Invoke(selectItem, param); var key = Expression.Invoke(selectKey, item); var lambda = Expression.Lambda<Func<TEntity, TKey>>(key, param); return source.OrderBy(lambda); 

    Here’s example usage on Northwind:

    using(var ctx = new MyDataContext()) {     ctx.Log = Console.Out;     var rows = ctx.Orders.OrderBy(order => order.Customer,         customer => customer.CompanyName).Take(20).ToArray(); } 

    With TSQL (reformatted to fit):

    SELECT TOP (20) [t0].[OrderID], -- snip FROM [dbo].[Orders] AS [t0] LEFT OUTER JOIN [dbo].[Customers] AS [t1]   ON [t1].[CustomerID] = [t0].[CustomerID] ORDER BY [t1].[CompanyName] 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has

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.