0

This feel really simple but I tried many scripts and nothing works. I have an interface with Id field - named ITrust, And I have a dictionary - Dictionary<string, Ilist<ITrust>>. Now I'm trying to remove a single object from the list inside the dictionary but can't find the right script.

I tried some things like that:

dictionaryName[keyName].Remove(x => x.Id == Id);

But it raise an exception -

Cannot convert lambda expression to type ITrust because it is not a delegate type

Any one know the right script or maybe if there is a problem with might I try to do?

Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
NGDeveloper
  • 160
  • 10

1 Answers1

4

First off, that's a compilation error, not an exception. And second, stop using IList<> if you don't understand what it means.

If you use a normal List<>, you have a List.RemoveAll function that does exactly what you want.

Blindy
  • 65,249
  • 10
  • 91
  • 131
  • 1
    Also, based on the fact that you need this, I very much doubt that a list is the correct data structure for you. You should probably be using a `HashSet<>` instead. – Blindy Jan 06 '23 at 16:10
  • He seems to use IList not IList. – Ralf Jan 06 '23 at 16:10
  • @Ralf, I don't believe you, the error message is strongly typed. Do you have any backing evidence to your claim? – Blindy Jan 06 '23 at 16:11
  • No. Just what he wrote. – Ralf Jan 06 '23 at 16:12
  • Thanks @Blindy, That did work although I do need IList for testing but I will manage to find a way around it. anyway you helped me a lot. Thanks! – NGDeveloper Jan 06 '23 at 16:15
  • 3
    I don't see why they shouldn't use `IList`. Even if `List` is better, the remark is quite condescending. – Gert Arnold Jan 06 '23 at 16:21
  • @GertArnold: i agree about the remark, but i think `IList` is not the right collection here if he wants to remove items. Because `ITrust[]` would also implement `IList` but cause an exception at runtime because an array cannot be modified. I agree with Blindy that a `ISet` might be best, because it seems that an `Id` is a unique identifier and also because a set is more efficient then. – Tim Schmelter Jan 06 '23 at 16:27
  • That is correct, and is exactly the reason why `IList<>` is the wrong type to use: removing an element from a fixed size array makes no sense. – Blindy Jan 06 '23 at 18:40