Question

How do I convert the following to a Lambda?

Func<int, string> calcState = delegate(int test) 
    { 
        return (!MyList.All(i => i > test) ? 
            (MyList.Any(i => i > test) ? "ein Paar" : "Keiner") : "alle"); 
    };
Was it helpful?

Solution

Remove delegate and return keywords. Also you don't need to specify type of parameter - it will be inferred:

Func<int, string> calcState = 
   test => (!MyList.All(i => i > test) ? (MyList.Any(i => i > test) ? "ein Paar" : "Keiner") : "alle");

Further reading: Expression Lambdas

OTHER TIPS

Do the following:

Func<int, string> calcState = test => (!MyList.All(i => i > test) ? (MyList.Any(i => i > test) ? "ein Paar" : "Keiner") : "alle");

That's it.

It partially is already one, but thats what I would come up with:

Func<int, string> calcState = 
    test => (!MyList.All(i => i > test) ? 
    (MyList.Any(i => i > test) ? "ein Paar" : "Keiner") : "alle");

A Lambda is defined by the =>, so the i => i > test are already Lambda-Expressions.

P.S.: I hope this was just an example and in your real application you are pulling the strings from the resources (or similiar) and do not use hard-coded strings?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top