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

  • Home
  • SEARCH
  • 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 8623333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:15:22+00:00 2026-06-12T07:15:22+00:00

I am trying to sort a linq query so that the results are returned

  • 0

I am trying to sort a linq query so that the results are returned in the order of the IDs found in the List[int]. Here is my current code that returns them just fine, but not sorted.

IEnumerable<NPost> nposts;

List<int> npostIDs = (from tc in db.TopComs
                      where tc.Type == "Comments"
                      select tc.NPostID).ToList();

            nposts = from np in repository.NPosts
                     where npostIDs.Contains(np.NPostID)
                     select np;

How can I have it so that nposts returns the results in the order in which npostIDs exists in the List[int]?

  • 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-12T07:15:23+00:00Added an answer on June 12, 2026 at 7:15 am
    IEnumerable<NPost> nposts = from np in repository.NPosts
                                let index = npostIDs.IndexOf(np.NPostID) 
                                where index >= 0
                                orderby index ascending
                                select np;
    

    Update

    Based on your error, I’ve got another suggestion. I’m not 100% sure if it will work or not in EF, but give it a try and let me know. There’s one other idea I have that I know would work but it’s not going to perform as well.

    IEnumerable<NPost> nposts = from npostID in npostIDs.AsQueryable()
                                join np in repository.NPosts
                                on npostID equals np.NPostID
                                select np;
    

    This will maintain the order of the npostIDs without an orderby clause. If the ObjectContext is the same (and maybe if it’s not), you should actually be able to do it in a single query. However, it’s not clear if you are caching the npostIDs list or not, so this might not be an option. Anyway, here:

    IEnumerable<NPost> nposts = from tc in db.TopComs
                                where tc.Type == "Comments"
                                join np in repository.NPosts
                                on tc.NPostID equals np.NPostID
                                select np;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to sort a LINQ to SQL query based on two fields.
Im trying to use LINQ to sort data that is going to be used
I'm trying to write Linq query on this Products table based on FacetTypes that
I have a local collection that I am trying to sort with Linq but
I am trying to sort a xml using linq queries using the following code
I'm trying to sort a dictionary by value with LINQ but I can't figure
I am trying to sort a list of names followed by another string such
I am new to LINQ and am trying to sort this one out. I
i´m trying to sort a list of combinations in a specific way. Consider the
I'm trying to order the values in a related table using LINQ to SQL

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.