Click here to Skip to main content
15,900,724 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I need to check whether string is in list string or not, if not then I need to do something

C#
string fieldFromDB2;
List<string> commonMsgId = new List<string>();
commonMsgId.Add(fieldFromDB2);
if(!(fieldFromDB2==commonMsgId))
//do something


This code will work or not??


Thanks
John
Posted

C#
if (commonMsgId.IndexOf(fieldFromDB2) > 0)
      {
          //string exists in list
      }
      else {
          //string does not exists in list
      }
 
Share this answer
 
Comments
[no name] 16-Dec-13 7:18am    
>= ?
Add this namespace :
C#
using System.Collections.Generic;


Try like below :

C#
if (commonMsgId.Contains(fieldFromDB2))
{
    // string contains
}
else
{
  // not contains
}
 
Share this answer
 
''NOTE my Engli
sh is bad O.o

'from your question i assume this
dude, you have to use for loop to check all the string in the list.

void runData(){

string a;
List listStr=new List();
listStr.add("apple");
listStr.add("ball");
listStr.add("cat");
listStr.add("donkey");
listStr.add("elephant");


///Checking the data in the list

a="cat";

for(int i=0;i<=listStr.count-1;i++){

if(a==listStr[i]){
//Do something

//
}
else{
//Do something
}



}
 
Share this answer
 
C#
string fieldFromDB2 = "one";
List<string> commonMsgId = new List<string>() { "one", "three", };
bool areAllInList = commonMsgId.Contains(fieldFromDB2)
 
Share this answer
 
v2
Try the given below way -

C#
List<string> search = commonMsgId.FindAll(x => x == fieldFromDB2);</string>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900