c# – 如何在一个步骤中获取列表中的项目的索引?

如何在列表中查找项目的索引而不循环?

目前这看起来不是很好 – 搜索列表中相同的项目两次,只是为了获得索引:

var oProp = something;

int theThingIActuallyAmInterestedIn = myList.IndexOf(myList.Single(i => i.Prop == oProp));
怎么样List.FindIndex Method

int index = myList.FindIndex(a => a.Prop == oProp);

This method performs a linear search; therefore, this method is an
O(n) operation, where n is Count.

http://stackoverflow.com/questions/17995706/how-to-get-the-index-of-an-item-in-a-list-in-a-single-step

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:c# – 如何在一个步骤中获取列表中的项目的索引?