Merge two List<object> into one List in Linq

前端 未结 5 652
猫巷女王i
猫巷女王i 2020-12-05 15:12

I have two lists. BeamElevations and FloorElevations. How can I merge these into Elevations

5条回答
  •  天命终不由人
    2020-12-05 15:55

    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.

提交回复
热议问题