CanESER
CanESER

Reputation: 297

Lambda expression does not work in object list

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.

Upvotes: 0

Views: 205

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476594

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.

Upvotes: 2

Related Questions