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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:14:39+00:00 2026-05-26T14:14:39+00:00

I have an array of Ids, in order, using these ids I then issue

  • 0

I have an array of Ids, in order, using these ids I then issue a linq query, however I want the results of the linq query to come back in the same order that the Ids in the array are in.

Example:

int[] ids = new [] {10, 9, 8, 7};

var query = from row in context.Table where ids.Contains(row.Id) select row;

How could I ensure that the obtained items are in the same order as those in the array?

  • 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-26T14:14:40+00:00Added an answer on May 26, 2026 at 2:14 pm

    You can order the rows by the index of their Id in ids:

    var query = from row in context.Table
                where ids.Contains(row.Id)
                orderby Array.IndexOf(ids, row.Id)
                select row;
    

    I’m not sure if the LINQ-to-SQL provider supports this; you may need to perform the ordering using LINQ-to-Objects:

    var query = from row in context.Table
                where ids.Contains(row.Id)
                select row;
    
    var orderedQuery = from row in query.AsEnumerable()
                       orderby Array.IndexOf(ids, row.Id)
                       select row;
    

    or

    var orderedQuery = context.Table
                              .Where(row => ids.Contains(row.Id))
                              .AsEnumerable()
                              .OrderBy(row => Array.IndexOf(ids, row.Id));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given an array of ids $galleries = array(1,2,5) I want to have a SQL
I have an array of IDs that are already sorted in descending order as
I have an array of employee ids: 34 , 35, 40, 67, 54 and
I have an array full of random content item ids. I need to run
I have a set of ids and names in an associative array and in
I have a PHP array with numbers of ID's in it. These numbers are
I have an array with the ids of x cakes and another associative array
I have an unsorted Array holding the following IDs: @un_array = ['bar', 'para-3', 'para-2',
I have this php code $ids = array(1,2,3); $names = array(cat,elephant,cow); $originalSettings = array
If i have an array with hundreds of random ids (having values too) like

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.