26

I have two lists. BeamElevations<Elevation> and FloorElevations<Elevation>. How can I merge these into Elevations<Elevation> list and order them based on their Elevation using Linq?

Vahid
  • 5,144
  • 13
  • 70
  • 146
  • 1
    Didnt down vote but id imagine its because of the lack of research effort – Sayse Apr 11 '14 at 15:51
  • 2
    i think it's because people are nobs – Weyland Yutani Apr 11 '14 at 15:51
  • 5
    @Sayse Actually I searched SO before posting this. But I found the answer after posting the question. I thought it doesn't hurt for this to remain on the website. It will definitely help some lost guy some day in the future. – Vahid Apr 11 '14 at 15:53
  • 4
    First google result... http://stackoverflow.com/questions/4488054/merge-two-or-more-lists-into-one-in-c-sharp-net – Sayse Apr 11 '14 at 15:56
  • @Sayse Thanks. I didn't see that. – Vahid Apr 11 '14 at 15:57

5 Answers5

63

Use Concat and OrderBy

var result = list1.Concat(list2).OrderBy(x => x.Elevation).ToList();

If you want to remove duplicates and get an unique set of elements you can also use Union method:

var result = list1.Union(list2).OrderBy(x => x.Elevation).ToList();

In order to make it work properly you need to overide Equals and GetHashCode methods in your class.

Selman Genç
  • 100,147
  • 13
  • 119
  • 184
  • 4
    @Vahid yes you can but it will remove the duplicates if you override equals and gethascode for your class.It is different from Concat see the [documentation](http://msdn.microsoft.com/en-us/library/bb341731(v=vs.110).aspx) : _This method excludes duplicates from the return set._ – Selman Genç Apr 11 '14 at 15:33
  • I see. I'll stick with `Union` then. – Vahid Apr 11 '14 at 15:35
  • Some people have lots of free time on their hands :) – Vahid Apr 11 '14 at 15:49
  • I don't know, It seems somebody think that I see the Union in the other answer and added after that but I didn't even noticed it mentioned in another answer I include it because Vahid ask me in the comments – Selman Genç Apr 11 '14 at 15:55
  • 5
    As for the downvoters, I am sorry that in our community there are people who downvote without leaving a message, about what was wrong. I think that should be change. – Christos Apr 11 '14 at 15:56
  • 3
    @Vahid I don't know, you should try it and see.I would say Concat is faster because it justs merging the items without comparing them.Union performs much more work than Concat – Selman Genç Apr 11 '14 at 16:21
  • Thanks I guess it is. – Vahid Apr 11 '14 at 16:22
  • Why to use orderBy ? – Moeez May 28 '18 at 04:05
6

Initially you merge them like below:

Elevations=BeamElevations.Union(FloorElevations)
    .ToList();

Then

Elevations=Elevations.OrderBy(x=>x.Elevation)
    .ToList();

Or in one step:

Elevations=BeamElevations.Union(FloorElevations)
    .OrderBy(x=>x.Elevation)
    .ToList();

Another way to achieve this would be to use Concat

Elevations=BeamElevations.Concat(FloorElevations)
    .OrderBy(x=>x.Elevation)
    .ToList();
SwissCodeMen
  • 4,222
  • 8
  • 24
  • 34
Christos
  • 53,228
  • 8
  • 76
  • 108
3

Use List.AddRange

   list1.AddRange(list2);
   list1.OrderBy(l => l.Elevation);
huMpty duMpty
  • 14,346
  • 14
  • 60
  • 99
2
 List<Elevation> Elevations= FloorElevations.Concat(BeamElevations).ToList();
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
2

If there are any devs out there that might be looking for a solution in VB.NET, here you go:

Dim list1 As List(Of MyObject)
Dim list2 As List(Of MyObject)
Dim mergedAndSortedList As List(Of MyObject)

mergedAndSortedList = (From obj In list1.Union(list2)
                       Order By obj.MyPropertyToSortOn
                       Select obj).ToList()
StackOverflowUser
  • 945
  • 12
  • 10