var lst = new List<(int number, string country, string population)> { };
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 MakwanaPosted Feb 28, 2023, 3:34 AM
Hello Chris,
You can use
Existsmethod.Thanks
Naimish Makwana
Vishal JoshiPosted Feb 28, 2023, 6:52 AM
Hello Chris,
Yes. we can use Contains along with Any or Exists.
Please check below sample code.
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
Tuhin PaulPosted Feb 28, 2023, 1:53 AM
Hi Chris,
You can use LINQ to check if the 2nd column of "lst" contains "France". Here's an example:
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.