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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:13:32+00:00 2026-06-11T20:13:32+00:00

Possible Duplicate: .Union() changes the order of the items? According to this question (without

  • 0

Possible Duplicate:
.Union() changes the order of the items?

According to this question (without scenario/example; you should remove it, I can’t) this is my problem :

I’ve noticed that if I do a Union, than an Intersect between collections Attachment[], the order of the Items “can” change.

This is my code :

    GalleryDataClassesDataContext db = new GalleryDataClassesDataContext();
    List<Attachment> Allegati = db.ExecuteQuery<Attachment>("EXEC SelectAttachmentsByKey @Key={0}, @IDCliente={1}", new object[] { "", "47" }).ToList();
    List<Attachment> AllegatiPerCategorie = new List<Attachment>();

    AllegatiPerCategorie = AllegatiPerCategorie.Union(db.AttachmentAttachmentCategories.Where(aac => aac.IDAttachmentCategory == 72).OrderBy(p => p.Ordine == null ? 1 : 0).ThenBy(p => p.Ordine).Select(aac => aac.Attachment)).ToList();
    Allegati = Allegati.Intersect(AllegatiPerCategorie).ToList();

    count = 0;
    foreach (Attachment a in AllegatiPerCategorie)
    {
        Response.Write(count.ToString() + " - " + a.IDAttachment + "<br />");
        count++;
    }

    Response.Write("<br />### FILTERED ###<br /><br />");

    count = 0;
    foreach (Attachment a in Allegati)
    {
        Response.Write(count.ToString() + " - " + a.IDAttachment + "<br />");
        count++;
    }

And the output is :

0 - 6769
1 - 6792
2 - 6771
3 - 6699
4 - 6632
5 - 6774
6 - 6595
7 - 6602
8 - 6641
9 - 6643
10 - 6764
11 - 6634
12 - 6642
13 - 6660
14 - 6640
15 - 6665
16 - 6673
17 - 6767
18 - 6772
19 - 6766
20 - 6763
21 - 6768
22 - 6644
23 - 6635
24 - 6633
25 - 6793
26 - 6677
27 - 6608
28 - 6610
29 - 6558
30 - 6563
31 - 6631
32 - 6604
33 - 6606
34 - 6607
35 - 6596
36 - 6597
37 - 6598
38 - 6599
39 - 6600
40 - 6471
41 - 6470
42 - 6469
43 - 6601
44 - 6603
45 - 6663
46 - 6664
47 - 6645
48 - 6637
49 - 6638
50 - 6609
51 - 6611
52 - 6612
53 - 6613
54 - 6614
55 - 6615
56 - 6616
57 - 6617
58 - 6618
59 - 6619
60 - 6620
61 - 6622
62 - 6567
63 - 6568
64 - 6569
65 - 6570
66 - 6571
67 - 6572
68 - 6573
69 - 6575
70 - 6576
71 - 6577
72 - 6579
73 - 6580
74 - 6581
75 - 6582
76 - 6583
77 - 6584
78 - 6585
79 - 6586
80 - 6587
81 - 6588
82 - 6589
83 - 6590
84 - 6591
85 - 6592
86 - 6593
87 - 6594
88 - 6765

### FILTERED ###

0 - 6769
1 - 6792
2 - 6771
3 - 6699
4 - 6774
5 - 6595
6 - 6602
7 - 6634
8 - 6642
9 - 6640
10 - 6660
11 - 6665
12 - 6673
13 - 6772
14 - 6766
15 - 6768
16 - 6644
17 - 6635
18 - 6633
19 - 6793
20 - 6677

Well, notice for example the order of values 6660 and 6640 in the AllegatiPerCategorie list : 6660 before 6640 (at position 13 and 14).

Now, watch at the same values order on Allegati : 6640 is before 6660 (at position 9 and 10).

Why this behaviour? How can I fix it? Thank you

  • 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-11T20:13:33+00:00Added an answer on June 11, 2026 at 8:13 pm

    MSDN states:

    When the object returned by this method is enumerated, Union enumerates first and second in that order and yields each element that has not already been yielded.

    Here is a short example to demonstrate the behavior:

    new int[] {1}.Union(new int[] {1, 2, 3}) // returns: 1,2,3
    new int[] {2}.Union(new int[] {1, 2, 3}) // returns: 2,1,3
    new int[] {3}.Union(new int[] {1, 2, 3}) // returns: 3,1,2
    
    new int[] {1,3,5}.Union(new int[] {2, 4}) // returns: 1,3,5,2,4
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How can I understand nested ?: operators in PHP? Why does this:
Possible Duplicate: Can main function call itself in C++? I found this problem very
Possible Duplicate: Difference between a Structure and a Union in C I see this
Possible Duplicate: A question about union in C Assuming the following code: #include <stdio.h>
Possible Duplicate: Can a union be initialized in the declaration? I've looked all over
Possible Duplicate: What is the difference between Join and Union? How do we combine
Possible Duplicate: How can I convert a list<> to a multi-dimensional array? I want
Possible Duplicate: How can I combine multiple rows into a comma-delimited list in Oracle?
Possible Duplicate: php == vs === operator Reference - What does this symbol mean
Possible Duplicate: Can a Bash script tell what directory it's stored in? Is there

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.