Reputation: 1233
I have two lists:
public static List<Dinosaur> Dinosaurs = new List<Dinosaur>();
public static List<DinosaurSpecies> DinosaurSpeciesList = new List<DinosaurSpecies>();
I want to use the Species in the first list to find the key of the Species in the second list. The following throws a 'has some invalid arguments' but it does illustrate what I'm trying to do:
int index = MainWindow.DinosaurSpeciesList.FindIndex(MainWindow.Dinosaurs[i].Specie);
In other words, where does the Species in the Dinosaurs list [index] appear in the list of all DinosaurSpecies
?
Upvotes: 1
Views: 143
Reputation: 48415
You can do this by passing a predicate to the FindIndex
method:
int index = MainWindow.DinosaurSpeciesList.FindIndex(x => x.Specie == MainWindow.Dinosaurs[i].Specie);
Basically, you are saying: Find the index of the element whos Specie
property is equal to the specified Dinosaur.Specie
property
A simplified and more understandable example might be:
Dinosaur dinosaur = GetDinosaurToFindSpeciesInformationFor();
int index = DinosaurSpeciesList.FindIndex(x => x.Specie == dinosaur.Specie);
Of course, if you then plan to only use the index to get the DinosaurSpecies object anyway, you could do this:
DinosaurSpecies species = DinosaurSpeciesList.SingleOrDefault(x => x.Specie == dinosaur.Specie);
//NOTE: species will be null if there are no matches, or more than one match
Upvotes: 3
Reputation: 7559
Your parameters to FindIndex
are wrong. The single-parameter form requires a lambda (or more specifically, a Predicate<T>
):
int index = MainWindow.DinosaurSpeciesList.FindIndex(x => x.Specie.Equals(MainWindow.Dinosaurs[i].Specie));
Depending on what your DinosaurSpecies
class looks like, of course.
ps. I like dinosaurs
Upvotes: 2