0

I have an object list and I can add record with that sentence:

List<DragerClass.Alarm> alarms = new List<DragerClass.Alarm>();
public void createAlarm(int i, int[] alarms)
{
    alarms.Add(new DragerClass.Alarm(i, DateTime.Now, DragerClass.Dedector.Dedector_Name[i] + " UNDER RANGE"))`;
}

But when I try to remove an item, it behaves like lambda expression doesn't support:

public void removeAlarm(int i)
{
    alarms.Remove(x => x.Dedector_No == i);
}

I see that message when I stand on the code

cannot convert lambda expression to type 'Drager_GasDedection.DragerClass.Alarm' because it is not a delegate type

I'm using Visual Studio 2010 and I also added System.Data.Entity in references. But still same. Thanks for any help.

9
  • @ASh I tried that with RemoveAll now and the message is this now " 'Drager_GasDedection.DragerClass.Alarm' does not contain a definition for 'Dedector_No' and no extension method 'Dedector_No' accepting a first argument of type 'Drager_GasDedection.DragerClass.Alarm' could be found (are you missing a using...) Commented Jun 10, 2016 at 11:28
  • I think you need alarms.RemoveAt(i); Commented Jun 10, 2016 at 11:30
  • "i" is the number of item not the index of list so RemoveAt is not helpful @MikeDebela Commented Jun 10, 2016 at 11:32
  • 1
    @CanESER, i think you should explain what you are deleting (alarms or detectors) and describe your data structures. but using of Remove with predicate is incorrect Commented Jun 10, 2016 at 11:34
  • 3
    alarms.RemoveAll(x => x.Dedector_No == i); Commented Jun 10, 2016 at 11:47

1 Answer 1

2

Take a look at the methods of List<T>. The method Remove(T) simply expects one element. If it is found in the list it is removed, otherwise nothing is done. Remove is not looking for a Predicate<T> that it will check.

RemoveAll(Predicate<T>) however expects a predicate. So you need to call:

alarms.RemoveAll(x => x.Dedector_No == i);

You also have to change = to == in your code since otherwise you are performing an assignment instead of an equality check. Furthermore note that the method will remove all alarms with the given detector number, not just the first.

Sign up to request clarification or add additional context in comments.

1 Comment

In that list each dedector can max have one alarm so remove all is not problem and sorry for '=' it was actually what i wanted to type, im fixing now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.