Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# findindex with multiple expressions

Tags:

c#

How can I search a list for two different things?

For example, this code works perfectly fine:

int index = mylistofobjects.FindIndex(a => a.firstname == "Bob");

Is there a way to change it to something like this?

int index = mylistofobjects.FindIndex(a => a.firstname == "Bob", a.lastname == "Smith");

I'm trying to return the mylistofobjects instance that has first name Bob and lastname Smith.

Thanks for the help.

like image 942
carlg Avatar asked Feb 22 '23 18:02

carlg


2 Answers

Try this:

int index = mylistofobjects.FindIndex(a => a.firstname == "Bob"&& a.lastname == "Smith");

like image 106
kleinohad Avatar answered Mar 03 '23 09:03

kleinohad


Use && operator.

int index = mylistofobjects.FindIndex(a => a.firstname == "Bob" && a.lastname == "Smith"); 
like image 37
Per Kastman Avatar answered Mar 03 '23 11:03

Per Kastman