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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:46:17+00:00 2026-05-16T20:46:17+00:00

Update: False alarm! The source of the error was elsewhere. (See at the end

  • 0

Update: False alarm! The source of the error was elsewhere. (See at the end of the question.)

Is it possible that either Linq or foreach can mess up the order of an array?

One of my testers reported to have experienced that the order of a list of items he fed in as input didn’t match the order of the final list that was saved in the database. More precisely, the first element became the last.

So I reviewed my code step by step, and I have no clue what should change the order. There is a Linq query and a foreach loop however. Is it possible that one of these can mess up the order of an array?

Code, simplified:

List<FooBar> fooBarList = new List<FooBar>();

string[][] theData = new string[][] { 
    new string[] { "a", "x" },
    new string[] { "b", "y" },
    new string[] { "c", "z" } };

FooBar[] fooBarArray = theData.Select(
    row => new FooBar { Foo = row[0], Bar = row[1] }
    ).ToArray();

foreach (FooBar item in fooBarArray)
{
    int iRank = fooBarList.Count + 1;
    item.Ranking = iRank;
    fooBarList.Add(item);
}

The array of arrays of strings theData is in fact given as an input. It is transformed into an array of business objects. These are then added to a list and assigned a ranking field. This field is written to the database together with “Foo” and “Bar”.

After saving the list in the database, the rank of “a” was 3 in that particular case. For me, however, I cannot reproduce the misbehavior…

Update: I was wrong, the data written to the database was correct. The data I looked to was from a business object that was copied from the original one. When copying, the order was mixed up while reading it from the database, and this wrong order was then persisted in the copy of the object… => Accepted Jon’s answer saying “LINQ to Objects generally has a predictable ordering – other providers often don’t.”

  • 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-16T20:46:18+00:00Added an answer on May 16, 2026 at 8:46 pm

    Well, your sample code is only showing LINQ to Objects. How are you inserting the data into the database? If that’s using LINQ as well, I strongly suspect that it’s the LINQ to SQL (or whatever) side which is causing the issue, not LINQ to Objects.

    LINQ to Objects generally has a predictable ordering – other providers often don’t.

    EDIT: If this happens reproducibly, then you ought to be able to catch it happening in the debugger… that should give you some hints. I suspect that if you try to create a short but complete program which demonstrates the problem, you’ll end up finding out what’s wrong.

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

Sidebar

Related Questions

Update My problem was that I used event.preventDefault() instead of return false to hinder
I call window.location.reload(false) in a javascript method to update the page. I have additional
Update 2018 : This question was asked long before PostCSS existed, and I would
Update: The index.php file here: /public_html/d/index.php includes: /public_html/d/core/source/class.File1.php This Class.File1.php here has this include
UPDATE The second after I posted this question, thanks to the syntax highlighting of
UPDATE: I found that the easiest way is to use AQuery . I need
var dlg = $(#dialog).dialog({ autoOpen: false, modal: true, buttons: { 'Update': function() { alert(clientCode);
Update 2: Seems that this library is not Compact-Framework aware at all and I
How can I use contains method on a list that contains HashMaps so that
Below is my RUN_SQL function: def RUN_SQL_SAFE(sql, input_tuple=(), get_update_id=False, debug = False): conn =

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.