C#
var lst = new List { };
lst.Add((4, "Germany", "83 Millionen"));
lst.Add((3, "USA", "328 Millionen"));
lst.Add((1, "France", "66 Millionen"));
lst.Add((2, "Spain", "46 Millionen"));Hi,
How do I check if the 2nd column of "lst" contains "France" ? (for example)
lst.Contains( ?
Thanks!!

Naimish MakwanaRank #324 · 7.7K reputationPosted Feb 27, 2023, 10:34 PM EST
Hello Chris,
You can use
Existsmethod.bool isExist = lst.Exists(item => item.country == "France");Thanks
Naimish Makwana
Tuhin PaulRank #7 · 71.9K reputationPosted Feb 27, 2023, 8:53 PM EST
Hi Chris,
You can use LINQ to check if the 2nd column of "lst" contains "France". Here's an example:
bool containsFrance = lst.Any(x => x.country == "France");This code uses the Any extension method to check if any element in the list satisfies the condition x.country == "France". If at least one element satisfies the condition, containsFrance will be set to true; otherwise, it will be set to false.
You can modify the condition to check for other countries or to use a different comparison operator if needed.
Vishal JoshiRank #377 · 6.7K reputationPosted Feb 28, 2023, 1:52 AM EST
Hello Chris,
Yes. we can use Contains along with Any or Exists.
Please check below sample code.
lst.Any(a => a.country.Contains("France"))Reason to use Contains along with Any.
In case to check country like USA. some people use to say US and some say USA.
To Handle this type of case it would be helpful to use Contains along with Any.
Thanks
Vishal