1

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

3 Answers 3

8
Action<ItemType> action = item => { if(!Reports.Contains(item)) Reports.Add(item);};

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

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

Comments

5

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

1 Comment

Agreed. It sounds like a different data structure may be needed.
0

you can separate multiple lines in your lambda with semicolons.

Comments

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.