Malai
Malai

Reputation: 63

How do i check if a particular string is not present in a list in c#?

I have a list as follows,

List<string> variablelist = new List<string> { "first", "second","third"};

And i have one more list, like

 List<string> method = new List<string>();
 //somecode
 method.Add(workingline);

I want to check if any of the element of variablelist is NOT present in methodlist and also I want get that Particular ELEMENT.

Thanks in Advance

Upvotes: 1

Views: 692

Answers (2)

Bernard Walters
Bernard Walters

Reputation: 391

this should be another solution to it.

 var list = variablelist.Where(ItemName => !method.Contains(ItemName)).ToList();

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1499790

LINQ is the simplest way of doing this, with the Except method:

var inOnlyVariableList = variableList.Except(method).ToList();

The result will be a List<string> of strings which are in variableList but not in method.

Upvotes: 11

Related Questions