How to check if a value exists in a list?

I'm relatively new to C # programming (programming in general, actually), but I created an application to manage application pools on a server that uses my team at work. It does everything that should be good enough, but the only problem that I encountered is to save the previously used configurations in the app.config file, so the user does not need to enter them manually each time. Be that as it may, I can save and load from the file superbly (along with all the lines that I need in each group).

The problem is that I want to perform a surface check to see if the name string exists in the group before it is written. An example of the app.config part:

<appSettings>
 <add Name="RowName" MachineName="MS-02348" AppSrvName="AppServer" WebSrvName="AppNet"/>
 <add Name="RowName2" MachineName="MS-68186" AppSrvName="AppServer2" WebSrvName="AppNet2"/>
</appSettings>

So, what am I doing now to load values, I have a method that extracts the appSettings / add nodes and lists them, and then sets the values ​​to the properties of the object. The reason I do this is because I can have a drop-down list that only shows the name of the object, and then all the rest of the information is available when I call the method on the selected item.

In any case, now I want to make sure that if the name already exists in app.config, I suggest the user to write a different name instead of saving it to the database. Having two child nodes with the same "Name" value will cause chaos in my logic.

foreach , , , , , . childnode , node, , , . , , , , , .

?

+3
3
if (list.Any()) 
{ 
   // found something! 
}
else 
{
   // found nothing 
}

Any() , . List.Count() , - , . Any() , . , , Count() , Any() .

, bool, .:)

Any() . , , 21 :

if (list.Any(person => person.Age > 21))
{
   // ...
}

: .

+7

-

        var list = new List<AppSettings>();
        var item = list.FirstOrDefault(x => x.Name == NameEnteredByUser);
        if (item == null)
        {
            //there is no such item
        }
        else
        {
           //notify the user
        }

:

 var list = new List<AppSettings>();
        if (list.Any(x => x.Name == NameEnteredByUser))
        {
            //name exists
        }
        else
        {
            //no such name used before
        }

, , , , , . db .

+5

neo112 , , , , , , , .

-, :

int count = list.Count(a => a.Name == NameEnteredByUser);
if(count > 0)
{
    // exists
}

, .Count() , .First() ( ) .

, , , - appSettings node. SortedList, List, . , - .

0

All Articles