I have list of string Like list1 contain 'pinky','smita','Rashmi','Srivani' And string is like String str = "pinky, Nandini' I want to check if neither of str present in list1,proceed further. how to do that?
CodePudding user response:
If I understood correctly, you want to return false in the example case, so you can use Any
method: Check if none of the elements of the list is already in the str
, here is a one liner:
if (!list.Any(x=>str.Contains(x))) ....
CodePudding user response:
You can use combination of .Any()
with .Contains()
with !
,
var list1 = new List<string>(){ "pinky", "smita", "Rashmi", "Srivani" };
string str = "pinky, Nandini";
var list2 = str.Split(",");
var nameExists = list2.Any(x => list1.Contains(x));
if(!nameExists)
{
//Your code goes here.
}
As @Fildor said, you can use Intersect()
. Elegant approach,
//All credit goes to @Fildor
var nameExists = list1.Intersect(list2).Any();