Remove instances from a list by using LINQ or Lambda?

前端 未结 7 1583
囚心锁ツ
囚心锁ツ 2021-02-05 03:26

Now I come a stage to get all my data as a list in cache(objects) and my next thing I have to do is to remove some instances from the list.

Normally, I would do removing

相关标签:
7条回答
  • 2021-02-05 04:01

    Maybe you're trying to do something like this?

    List<T> firstList;
    List<T2> toBeRemovedItems;
    List<T> finalList;
    
    foreach(T item in firstList)
    {
        toBeRemovedItems = CheckIfWeRemoveThisOne(item.Number, item.Id);
        if (toBeRemovedItems == null && toBeRemovedItems.Count() == 0)
            finalList.Add(item);
    }
    

    This is how I managed to solve an issue with getting rid of duplicates between a List<ViewModel> and a List<Model>. I used the CheckIfWeRemoveThisOne function to check if the item.Number belonged to some other item, using the ID as the defining characteristic. If it found another item (a duplicate), rather than try and remove it from the original list (which I was getting back a List<Model> and was given a List<ViewModel> into my function in the first place, so I had my doubts as to how I could do it, anyway), I just built a new list -- adding the result into it if it was found to be ok.

    0 讨论(0)
提交回复
热议问题