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

前端 未结 9 1670
[愿得一人]
[愿得一人] 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:05

    Given you override the .Equals() method for Car to determine one car object is the same as another car object, then the following should work w/o writing an extension method.

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

提交回复
热议问题