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

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

    Assuming your Get*Cars() return Lists of Car, another option could be:

    var list = new List();
    GetGreenCars().ForEach(c => { if (!list.Contains(c)) list.Add(c); });
    GetBigCars().ForEach(c => { if (!list.Contains(c)) list.Add(c); });
    GetSmallCars().ForEach(c => { if (!list.Contains(c)) list.Add(c); });
    

提交回复
热议问题