user2254463
user2254463

Reputation: 11

Find and return matched string from List

I have a list of string and gridview rows where I need to find if row contains any of the string from my list and return the matched string.

This is my list sample:

List<string> lstFind = new List<string>() { "TXT1", "TXT2", "TXT3", "TXT4" };

Then I need to check if row contains any of the string from my list above, something like...

lstRemovecol.Any(row["Item code"].ToString()

How do I accomplish that?

Thank you in advance.

Upvotes: 0

Views: 231

Answers (2)

Jon Skeet
Jon Skeet

Reputation: 1500015

If you're looking for an exact match, you just need:

bool found = lstRemovecol.Contains(row["Item code"].ToString());

No need for LINQ at all, or Any.

If you're trying to find any of the items within a longer string (for example, if the entry was "This is TXT1 that you should find", you'd want something like:

string code = row["Item code"].ToString(); // Or use a cast
bool found = lstRemovecol.Any(item => code.Contains(item));

Upvotes: 2

Misters
Misters

Reputation: 1347

lstRemovecol.Any(x=> x == row["Item code"].ToString())

or

lstRemovecol.Any(x=> x.Equals( row["Item code"].ToString()) )

Upvotes: 2

Related Questions