Get index of object in a list using Linq [duplicate]

删除回忆录丶 提交于 2019-11-28 20:06:27

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

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

Linq to Objects has overloaded Select method

customers.Select((c,i) => new { Customer = c, Index = i })
         .Where(x => x.Customer.ID == 150)
         .Select(x => x.Index);

Keep in mind, that you should have in-memory List<Customer> to use this Linq to Objects method.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!