Remove instances from a list by using LINQ or Lambda?

前端 未结 7 1603
囚心锁ツ
囚心锁ツ 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 03:51

    I agree with Jared's suggestion of filtering out certain items, but it looks like a join on Value1 would be a more efficient approach:

    var res = from item1 in list
              join item2 in toBeRemovedList
                on item1.Value1 equals item2.Value1
              where item1.Value2 >= item2.Value2
              select item1;
    

    Update: Apparently I fail at reading comprehension - new approach:

    var removeDict = toBeRemovedList.ToDictionary(i => i.Value1, i => i.Value2);
    list.RemoveAll(item => {
        int itemToRemoveValue2;
        if(removeDict.TryGetValue(item.Value1, out itemToRemoveValue2))
            return item.Value2 < itemToRemoveValue2;
        return false;
    });
    

    Of course, it would be even better if your list to remove could start as a dictionary. Ultimately, we're just trying to make our match on Value1 more efficient.

    0 讨论(0)
  • 2021-02-05 03:52

    You could use the method RemoveAll:

    MyClass one; //initialize MyClass
    list.RemoveAll(item => one.Value1 == item.Value1 && one.Value2 < item.Value2);
    
    0 讨论(0)
  • 2021-02-05 03:54

    You can use LINQ's Where method to filter out values that should not be a part of the list. The result is an IEnumerable<T> with the elements removed.

    var res = list.Where(item => !(one.Value1 == item.Value1 && one.Value2 < item.Value2));
    

    This will not updated the original List<T> instance but instead will create a new IEnumerable<T> with the values removed.

    0 讨论(0)
  • 2021-02-05 03:55

    If I get the question correctly, to produce a unique set from two List.

    For this, you can use the following

    List list1; List list2;

    List list3 = list1.Except(list2)

    The list3 will contain unique items.

    0 讨论(0)
  • 2021-02-05 03:55
    foreach(var item in toBeRemovedLItems) {   
       list.RemoveAll(one => one.Value1 == item.Value1 && one.Value2 < item.Value2); 
    }
    

    Too late again. Oh well.

    0 讨论(0)
  • 2021-02-05 04:00

    For collections that are not lists (can't expose RemoveAll), you can still remove items with a one-liner.

    To replace inline, just generate a list of items to remove, then run through it and execute remove code.

    var dictionary = new Dictionary<string, string>(){{"foo", "0"}, {"boo", "1"}, {"goo", "1"}};
    dictionary
        .Where(where_item =>
            ((where_item.Key == "foo") && (where_item.Value == "0"))
            || ((where_item.Key == "boo") && (where_item.Value == "1"))
        )
        .ToList()
        .ForEach(remove_item => {
            dictionary.Remove(remove_item.Key);
        });
    

    To replace in copy, just generate a filtered enumerable and return a new copy.

    var dictionary0 = new Dictionary<string, string>(){{"foo", "0"}, {"boo", "1"}, {"goo", "1"}};
    var dictionary1 = dictionary0
        .Where(where_item =>
            ((where_item.Key == "foo") && (where_item.Value == "0"))
            || ((where_item.Key == "boo") && (where_item.Value == "1"))
        )
        .ToDictionary(each_item => each_item.Key, each_item => each_item.Value);
    
    0 讨论(0)
提交回复
热议问题