tags:

views:

99

answers:

3

I have a lambda expression that currently looks like this:

item => Reports.Add(item)

I want to modify it such that it is conditional, and basically checks that Reports.Contains(item) returns false, then performs the Reports.Add(item) action. Is this possible to do using lambda all on one line?

Chris

+7  A: 
Action<ItemType> action = item => { if(!Reports.Contains(item)) Reports.Add(item);};

That should do, but it depends on how you define 'one line', really.

Ani
He didn't say that it has to be pretty or very readable :)
Daniel Mošmondor
Thanks - that worked perfectly!
Chris
+5  A: 

alternative to Ani's suggestion: make Reports a HashSet.

Jimmy
Agreed. It sounds like a different data structure may be needed.
Jace Rhea
A: 

you can separate multiple lines in your lambda with semicolons.

Mike Cheel