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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:27:54+00:00 2026-05-26T15:27:54+00:00

I’m trying to Order my main table(Contract) by some value in another table (via

  • 0

I’m trying to Order my main table(Contract) by some value in another table (via Join) then reduce it to Contracts again and sort it (ThenBy) by a value in Contract.

Here’s the short version of my question:
Is it possible to reduce a composite Anonymous type (produced by a Join) to a defined type without turning my IOrderedQueryable into an IQueryable?

Long version of the question with example:

My production code is quite complex but for the purpose of this question I’ve broken it down to a simple example:

static void simple()
{
  bool condition = true;

  var ctx = new LxDataContext();

  IQueryable<Contract> q1 = ctx.Contracts.Join(ctx.ContractDetails, c => c.ContractId, cd => cd.ContractId, (c, cd) => new { c, cd }).
                            Where(J => J.cd.DetailNo > 0).
                            Select(J => J.c);

  IOrderedQueryable<Contract> qOrdered;

  if (condition)
  {
    qOrdered = (IOrderedQueryable<Contract>)q1.Join(ctx.ContractPartners, c => c.ContractId, cp => cp.ContractId, (c, cp) => new { c, cp }).
               Join(ctx.VwPartners, J => J.cp.PartnerId, p => p.PartnerId, (J, p) => new { J.c, p }).
               OrderBy(J => J.p.LastName).
               Select(J => J.c);
  }
  else
    qOrdered = q1.OrderBy(c => c.Premium);

  IOrderedQueryable<Contract> qReady = (qOrdered).ThenBy(c => c.ContractId); //if condition == true exception here

  var dump = qReady.Skip(50).Take(50).ToList();
}

When condition is false everything works as excepted.
However when the more complex OrderBy() is executed the ThenBy() fails with this exception:

Expression of type 'System.Linq.IQueryable`1[InfoServiceTests.DB.Contract]' cannot be used for parameter of type 'System.Linq.IOrderedQueryable`1[InfoServiceTests.DB.Contract]' of method 'System.Linq.IOrderedQueryable`1[InfoServiceTests.DB.Contract] ThenBy[Contract,Int32](System.Linq.IOrderedQueryable`1[InfoServiceTests.DB.Contract], System.Linq.Expressions.Expression`1[System.Func`2[InfoServiceTests.DB.Contract,System.Int32]])'

So even though I cast it to IOrderedQueryable<> qOrdered doesn’t seem to be IOrderedQueryable<> and ThenBy() fails. Is it possible to sort by something in another table then reduce the result to Contract and ThenBy-sort it again?

Some comments:
– If it’s any help to you: In my production code i have a generic sorting class mostly taken from this answer.
– Yes I know the example could easily be fixed, but in my Production code I need a generic solution for sorting by user-defined values. This invloves looping and passing the Queryables around to functions.

  • 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-26T15:27:55+00:00Added an answer on May 26, 2026 at 3:27 pm

    When Condition is OK you should add some ordering becausae after select your query is not ordered query and ThenBy just works after ordered query:

    Performs a subsequent ordering of the elements in a sequence in
    ascending order.

    for example in your true condition you can do (in your if condition):

    qOrdered = (IOrderedQueryable<Contract>)q1
               .Join(ctx.ContractPartners, 
                c => c.ContractId, cp => cp.ContractId, (c, cp) => new { c, cp }).
                Join(ctx.VwPartners, J => J.cp.PartnerId, 
                     p => p.PartnerId, (J, p) => new { J.c, p }).
                   OrderBy(J => J.p.LastName).ThenBy(J=>J.ContractId)
                   Select(J => J.c);
    

    and you can do (in your else statement):

    qOrdered = q1.OrderBy(c => c.Premium).ThenBy(c=>c.ContractId);
    

    Also you should remove your current ThenBy operation after your if statements.

    In fact you should find something which is orderable and OrderBy it, then use ThenBy.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

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
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
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 am trying to loop through a bunch of documents I have to put

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.