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
MSDN states:
Here is a short example to demonstrate the behavior: