Not reproducible:
using System;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
var animals = new List<List<string>>()
{
new List<string> { "Elephant", "26", "Thailand" },
new List<string> { "Elephant", "40", "Thailand" },
new List<string> { "Ant", "2", "Australia" },
new List<string> { "Camel", "1", "Nigeria" }
};
int index= animals.FindIndex(r => r.Contains("Camel"));
Console.WriteLine(index);
}
}
That prints 3, as expected. You are doing something wrong in code you have not shown us. Show us the code that is failing. Take the code above that I just wrote, which is a complete program that you can cut-n-paste and execute, and modify it so that it demonstrates your bug. By doing so, you will either find the bug, or you will write a program where it is clear to others what the bug is.
UPDATE: The problem is not as stated. In the future, please write questions that describe the problem you actually have. That saves everyone involved from wasting time looking at the wrong problem.
The real problem is:
var animals = new List<List<string>>()
{
new List<string> { "Elephant", "26", "Thailand" },
new List<string> { "Elephant", "40", "Thailand" },
new List<string> { "Ant", "2", "Australia" },
new List<string> { "Camel red hump", "1", "Nigeria" }
};
And we wish to find item 3 based on the query string "Camel".
Here's how you attack this problem. First, you solve it for a single string:
string test1 = "Foo Bar Blah";
string test2 = "Camel red hump";
What do we want? We want to know if "Camel" appears in a string. That is done with string.Contains:
Console.WriteLine(test1.Contains("Camel")); // False
Console.WriteLine(test2.Contains("Camel")); // True
All right. We can solve the problem for a single string. Now solve the problem for a list of strings:
var list1 = new List<string> { "Ant", "2", "Australia" };
var list2 = new List<string> { "Camel red hump", "1", "Nigeria" };
We wish to know if the predicate string.Contains("Camel") is true for any member of list1 or list2. So use Any:
(Remember to add using System.Linq; to use the Any sequence method.)
Console.WriteLine(list1.Any(s => s.Contains("Camel")); // False
Console.WriteLine(list2.Any(s => s.Contains("Camel")); // True
Great, we now have a predicate that applies to lists of strings. Now we wish to apply that to a list of lists of strings:
var index = animals.FindIndex(
list => list.Any(
s => s.Contains("Camel")));
// Prints 3
You see what I did there? The methodology you should use for attacking problems on sequences-of-sequences is to solve the problem on individual elements first, then extend that solution to sequence-of-elements, and then extend that solution to sequence-of-sequence-of-elements, and so on. Build up from simple problems to harder problems.
Now, all that said, Any is probably the wrong predicate to use. If you have an animal new List<string>{"Canada Goose", "123", "United States"} and an animal new List<string>{"Wolverine", "4567", "Canada"} you run the risk of searching for Canada and finding the wolverine when you are looking for the Canada goose. You should probably not be representing this data as List<List<string>> in the first place. You should be doing this:
class Animal
{
public string Kind { get; }
public int Size { get; }
public string Country { get; }
public Animal (string kind, int size, string country)
{
this.Kind = kind;
... and so on.
Now you can say:
var animals = new List<Animal>()
{
new Animal("Elephant", 26, "Thailand"),
new Animal("Elephant", 40, "Thailand"),
new Animal("Ant", 2, "Australia"),
new Animal("Camel red hump", 1, "Nigeria")
};
And now your test is:
int index = animals.FindIndex(a => a.Kind.Contains("Camel"));
which is much easier to understand. Build types that represent concepts in your business domain. That will allow you to build programs that look like the concepts in your code, and not the mechanisms.
.FindIndex().forloop to scan the entire list to find your index and break once you found it