linq可以用某种方式来查找数组中值的索引吗?
例如,此循环在数组中定位键索引.
for (int i = 0; i < words.Length; i++)
{
if (words[i].IsKey)
{
keyIndex = i;
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个自定义对象列表,每次删除一个对象我需要知道它的索引时,有两个属性作为标识符(IDa,IDb).如何在不循环所有列表的情况下获取对象的索引?
List<CustomObject> list =new List<CustomObject>();
list.RemoveAll((MiniMapRecord p) => p.IDa == IDa.SystemID & p.IDb == pInputRecordMap.IDb);
Run Code Online (Sandbox Code Playgroud) 我想在char数组中找到一个字符的索引.我写下面的代码,但我想用一些library function/ LINQ来查找索引,而不是手动循环它.有没有smarter way/ concise way我能做到的.
尝试:
var index = -1;
char[] x = { 'A', 'B', 'C', 'D' , 'E'};
for(int t = 0; t< x.Length; t++)
{
if (x[t] == 'E')
index = t;
}
Console.WriteLine($"Index is: {index}");
Run Code Online (Sandbox Code Playgroud)