문제

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"); 
    };
도움이 되었습니까?

해결책

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

다른 팁

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?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top