Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert foreach loop to lambda expression

Tags:

c#

asp.net

lambda

I have this piece of code.

I want to convert this into lambda expression. How to reach this

string[] MyString= ConfigurationManager.AppSettings["MyStringData"].Split(',');

foreach (string res in MyString)
{
    if (res != "MyName")
    {
        btnshowname.Visible = false;
    }
    else if (res == "MyName")
    {
        btnshowname.Visible = true;
        break;
    }
}

Tell me the lambda expression please and how to achieve this

like image 374
Saurabh Sharma Avatar asked Jan 22 '14 11:01

Saurabh Sharma


1 Answers

btnshowname.Visible = MyString.Any(s => s == "MyName");
like image 95
Andrei Avatar answered Nov 10 '22 21:11

Andrei