Is there an AddUnique method similar to Addrange() for alist in C#

前端 未结 9 1511
[愿得一人]
[愿得一人] 2021-01-07 16:32

I have a list in C#:

       var list = new List();
       list.AddRange(GetGreenCars());
       list.AddRange(GetBigCars());
       list.AddRange(         


        
9条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-07 17:04

    One choice is to add them and remove the repeated ones:

    var list = new List();
    list.AddRange(GetGreenCars());
    list.AddRange(GetBigCars());
    list.AddRange(GetSmallCars());
    list = list.Distinct().ToList();
    

提交回复
热议问题