Get index of object in a list using Linq

RookieAppler picture RookieAppler · Aug 7, 2013 · Viewed 56.4k times · Source

I am new to Linq. I have a Customers table.ID,FullName,Organization,Location being the columns. I have a query in Sqlite returning me 2500 records of customers. I have to find the index of the customer where ID=150 for example from this result set. Its a List of Customers. The result set of the query is ordered by organization. I tried with FindIndex and IndexOf but getting errors for the former and -1 for the latter. So, how should it be done? Thanks.

Answer

Tim Schmelter picture Tim Schmelter · Aug 7, 2013

You don't need to use LINQ, you can use FindIndex of List<T>:

int index = customers.FindIndex(c => c.ID == 150);