Question List (multidimentional) : How to check if an item exists in the 2nd column?

vindiou

New member
Joined
Dec 20, 2019
Messages
3
Programming Experience
Beginner
C#:
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!!
 
Lists do not contains columns. What you have there is a list of tuples. You are asking how to check if second element of the tuple is the value "France". Anyway you would do the same thing you would normally do: iterate over the list. Or are you asking for a liner LINQ way of doing things so that LINQ does the iteration for you?
 
You can't use Contains because that looks for an item. You would use Any, which allows you to specify any condition to match on. You obviously want to match on the country property being equal to a particular value.
 
As per jmc, contains looks for an item that is equal to the item passed in:

C#:
lst.Contains((1, "France", "66 Millionen")); //true
lst.Contains((2, "France", "99 Millionen")); //false

You can use Exists, which is a non LINQ method supported by List to determine if any item in the list matches a predicate

C#:
lst.Exists(item => item.country == "France"); //true
You can also use LINQ's Any in exactly the same way, by doing using System.Linq at the top of the code and replacing Exists for Any
 
Consider naming your tuple items using PascalCase, so string Country - tuple names are essentially public properties and public things are PascalCase in C#
 
You can't use Contains because that looks for an item. You would use Any, which allows you to specify any condition to match on. You obviously want to match on the country property being equal to a particular value.

C#:
lst.Any(a => a.country.Contains("France"))
(someone gave me that code)
 
You're confusing string.Contains with List.Contains there.

When jmc said "you can't use Contains" they meant you cannot use List.Contains - this is a method that searches the list for an exact item specified. You'd have to pass in the exact tuple you want, and by default tuples are compared using equality on all members. I gave an example of what would, and would not, find your France


The code you have been given asks if Any list item has a country string that Contains the string "France". This is using string.Contains to assess whether string "France" is present within the .country of the tuple. This code potentially has bugs: if you asked for "Niger", and the list contained "Nigeria" but not "Niger", it would still report true because the string "Nigeria" does contain "Niger". If the match were case insensitive it gets worse; Somalia contains Mali, Romania contains Oman etc etc


You have stated you want to know if the list contains an item whose .country is "France". That is the code I gave:

C#:
lst.Exists(a => a.country == "France")

Or using Any, after importing System.Linq:

C#:
lst.Any(a => a.country == "France")

Note the lack of Contains; neither string.Contains nor List.Contains are used here because they don't seem appropriate. If you want case insensitive matching, consider something like:

C#:
lst.Any(a => "France".Equals(a.country, StringComparison.OrdinalIgnoreCase))

This is written the other way round deliberately; it will not crash if the country in the tuple is null.
 
Last edited:
C#:
lst.Any(a => a.country.Contains("France"))
(someone gave me that code)

Don't use that string.Contains method unless you specifically want to do a partial match. If you want to do a full match, use an equality operator or method.
 

Latest posts

Back
Top Bottom