-3
class parent

    public string a;
    public string b;
    // child fields
    public string c;
    public string d;
    
    public parent(string a, string b)
    {  
        this.a = a;
        this.b = b;
    }


class child1 : parent
    {
     public child1(string a, string b, string c) :base(string a, string b)
    }

class child2 : parent
    {
     public child2(string a, string b, string d) :base(string a, string b)
    }

IList<parent> parent_list = new List<parent>();
parent_list.Add(new child1("243", "ewfwe", "fewf"));
parent_list.Add(new child2("456", "fewf", "efew"));
parent_list.Add(new child1("123", "efe", "ewfew"));
parent_list.Add(new child2("768", "fewf", "ewf"));

var foo = 123

I want to delete all data for an object which has 123 as their field a. I cant seem to find the correct way to execute this. is it possible?

jaimy
  • 1
  • 1
  • Please have a look to the documentation first: [Documentation](https://learn.microsoft.com/de-de/dotnet/api/system.collections.generic.list-1.remove?view=net-5.0) ... Finding items in lists [here](https://stackoverflow.com/questions/16177225/find-element-in-list-that-contains-a-value) – Martin May 30 '21 at 00:01
  • https://stackoverflow.com/questions/853526/using-linq-to-remove-elements-from-a-listt – Zach Hutchins May 30 '21 at 00:03
  • I'm using ILIST though – jaimy May 30 '21 at 00:09
  • 2
    How about the count-backwards trick `for(var i = parent_list.Count - 1; i >= 0; i--) if(parent_list[i].a == "123") parent_list.Remove(i);` – Charlieface May 30 '21 at 00:37
  • Does this answer your question? [Unable to modify IList object's members](https://stackoverflow.com/questions/19408014/unable-to-modify-ilist-objects-members) – Jawad May 30 '21 at 01:06
  • @Jawad I don't think this is a duplicate of the question you linked (which is about modifying items, not the list). – Klaus Gütter May 30 '21 at 05:08

2 Answers2

0

Yes, you can apply this code.

parent_list = parent_list.Where(item => item.a != "123").ToList();

More details on Where

0

Just count backwards through the list, removing items as you go:

for(var i = parent_list.Count - 1; i >= 0; i--)
    if(parent_list[i].a == "123")
        parent_list.Remove(i);
Charlieface
  • 52,284
  • 6
  • 19
  • 43