Is it not allowed to have a conditional operator in a lambda expression in ForEach?
List<string> items = new List<string>{"Item 1", "Item 2", "Item I Care About"};
string whatICareAbout = "";
// doesn't compile :(
items.ForEach(item => item.Contains("I Care About") ?
whatICareAbout += item + "," : whatICareAbout += "");
Compilation error -> "Only assignment, call, increment, decrement, and new object expressions can be used as a statement"
Trying to use a normal if doesn't work either:
// :(
items.ForEach(item => if (item.Contains("I Care About")) {whatICareAbout += item + ", ";}
Just not possible?
You're using the shorter form of lambda expressions, which only allow a single expressions.
You need to the long form, which allows multiple statements.
For example:
What are you trying to acheive? Are you trying to form a string of comma separated items where they contain a particular value? In linq you would achieve this using the following:
The output from this is "Item I Care About, Item I Care About, Item I Care About".
Note: Aggregate is a great way of ensuring there is no trailing ","
Try parentheses:
+= has a higher precedence than ?, that may be why you're getting the error. With parentheses, the error may go away. Not 100% sure of this, though... lambda expressions may have additional restrictions which prevent use of assignment statements.
UPDATE:
Instead of multiple += statements, it's a lot cleaner to put the conditional on the right-hand side of the assignment, like this:
UPDATE 2:
But it's even better to just use Aggregate() since it's designed for exactly this scenario. Here's one sample:
But I think @Matt Breckon's answer above (that I just saw as I was about to post this)is even better than my example since it deals with removing the terminal ",". Look at his answer... :-)
The problem was that expression
is not a statement. It just returns a value which has type
string
.There is a trick to make it work (just for fun):
I simply added call to
.GetType()
method to create a statement from initial expression, and it compiled.