C# 使用foreach循环搜索数组并获取元素的索引
我有一门课是这样的:C# 使用foreach循环搜索数组并获取元素的索引,c#,arrays,C#,Arrays,我有一门课是这样的: public class MyClass { public char letter { get; set; } public double result { get; set; } public bool test { get; set; } } 我声明一个数组: MyClass[] myArray = new MyClass[counter]; 并用一些数据填充它 我对数组进行排序: myArray = myArray.OrderBy(a =&
public class MyClass
{
public char letter { get; set; }
public double result { get; set; }
public bool test { get; set; }
}
我声明一个数组:
MyClass[] myArray = new MyClass[counter];
并用一些数据填充它
我对数组进行排序:
myArray = myArray.OrderBy(a => a.letter).ThenByDescending(a => a.result).ToArray();
现在假设我有一个inti=100
变量
如何遍历此数组字段并获取第一个元素的索引:
test==false
结果
foreach(MyClass t in myArray.Where(a => a.letter == 'a')
{
if(t.result < i && t.test == false) get index of that field
}
var res = myArray
.Select((val, ind) => new {val, ind}))
.Where(p => p.val.result < i && p.val.letter == 'a' && !p.val.test)
.Select(p => p.ind);
foreach(myArray.Where中的MyClass t(a=>a.letter=='a'))
{
如果(t.result
但是,我不确定如何获取它的索引。我该如何做?您可以使用提供索引的
Select
重载来执行此操作,如下所示:
foreach(MyClass t in myArray.Where(a => a.letter == 'a')
{
if(t.result < i && t.test == false) get index of that field
}
var res = myArray
.Select((val, ind) => new {val, ind}))
.Where(p => p.val.result < i && p.val.letter == 'a' && !p.val.test)
.Select(p => p.ind);
var res=myArray
.Select((val,ind)=>new{val,ind}))
其中(p=>p.val.resultp.ind);
第一个
Select
将MyClass
对象(如val
)与其索引(如ind
)配对。然后,Where
方法表示三个条件,包括配对result
和ind
的条件。最后,最后一个Select
会删除MyClass
对象,因为不再需要它。您可以使用提供索引的Select
重载来完成此操作,如下所示:
foreach(MyClass t in myArray.Where(a => a.letter == 'a')
{
if(t.result < i && t.test == false) get index of that field
}
var res = myArray
.Select((val, ind) => new {val, ind}))
.Where(p => p.val.result < i && p.val.letter == 'a' && !p.val.test)
.Select(p => p.ind);
var res=myArray
.Select((val,ind)=>new{val,ind}))
其中(p=>p.val.resultp.ind);
第一个
Select
将MyClass
对象(如val
)与其索引(如ind
)配对。然后,Where
方法表示三个条件,包括配对result
和ind
的条件。最后,最后一个Select
会删除MyClass
对象,因为不再需要它。数组。FindIndex
应该可以为您解决以下问题:
int correctIndex = Array.FindIndex( myArray , item => item.letter == 'a' && item.result < i && !item.test );
int correctIndex=Array.FindIndex(myArray,item=>item.letter='a'&&item.result
第二个参数在功能上等同于在.Where()
子句中对其进行描述
此外,与类似的索引函数一样,如果找不到元素,它将返回
-1
。数组。FindIndex
应该可以为您解决以下问题:
int correctIndex = Array.FindIndex( myArray , item => item.letter == 'a' && item.result < i && !item.test );
int correctIndex=Array.FindIndex(myArray,item=>item.letter='a'&&item.result
第二个参数在功能上等同于在.Where()
子句中对其进行描述
此外,与类似的索引函数一样,如果找不到元素,它将返回
-1
。不带foreach
:
var item = myArray.FirstOrDefault(e => e.letter == 'a' && e.result < i && e.test == false);
int index = Array.IndexOf(myArray, item);
var item=myArray.FirstOrDefault(e=>e.letter='a'&&e.result
无需为每个人提供foreach
:
var item = myArray.FirstOrDefault(e => e.letter == 'a' && e.result < i && e.test == false);
int index = Array.IndexOf(myArray, item);
var item=myArray.FirstOrDefault(e=>e.letter='a'&&e.result
我看到这些人已经用更好的选择回答了你的问题,他们做得很好,但万一你还想知道如何为每个人做这些,下面是如何做的
int counter = 5 ; // size of your array
int i = 100 ; // the limit to filter result by
int searchResult = -1; // The index of your result [If exists]
int index = 0; // index in the array
MyClass[] myArray = new MyClass[counter]; // Define you array and fill it
myArray[0] = new MyClass {letter = 'f' ,result = 12.3 , test = false } ;
myArray[1] = new MyClass {letter = 'a' ,result = 102.3 , test = true} ;
myArray[2] = new MyClass {letter = 'a' ,result = 12.3 , test = false } ;
myArray[3] = new MyClass {letter = 'b' ,result = 88 , test = true } ;
myArray[4] = new MyClass { letter = 'q', result = 234, test = false };
myArray = myArray.OrderBy(a => a.letter).ThenByDescending(a => a.result).ToArray(); // Sort the array
foreach(MyClass t in myArray.Where(a => a.letter == 'a')) // The foreach part
{
if (t.result < i && t.test == false)
{
searchResult = index;
break;
}
index++;
}
// And finally write the resulting index [If the element was found]
int计数器=5;//数组的大小
int i=100;//过滤结果的限制
int searchResult=-1;//结果的索引[如果存在]
int index=0;//数组中的索引
MyClass[]myArray=新的MyClass[计数器];//定义数组并填充它
myArray[0]=newMyClass{letter='f',result=12.3,test=false};
myArray[1]=newMyClass{letter='a',result=102.3,test=true};
myArray[2]=newMyClass{letter='a',result=12.3,test=false};
myArray[3]=newMyClass{letter='b',result=88,test=true};
myArray[4]=newMyClass{letter='q',result=234,test=false};
myArray=myArray.OrderBy(a=>a.letter)。然后按降序(a=>a.result)。ToArray();//对数组进行排序
foreach(myArray.Where(a=>a.letter=='a')中的MyClass t)//foreach部分
{
如果(t.result
- 请注意:当然,结果索引将是排序数组中的索引
int counter = 5 ; // size of your array
int i = 100 ; // the limit to filter result by
int searchResult = -1; // The index of your result [If exists]
int index = 0; // index in the array
MyClass[] myArray = new MyClass[counter]; // Define you array and fill it
myArray[0] = new MyClass {letter = 'f' ,result = 12.3 , test = false } ;
myArray[1] = new MyClass {letter = 'a' ,result = 102.3 , test = true} ;
myArray[2] = new MyClass {letter = 'a' ,result = 12.3 , test = false } ;
myArray[3] = new MyClass {letter = 'b' ,result = 88 , test = true } ;
myArray[4] = new MyClass { letter = 'q', result = 234, test = false };
myArray = myArray.OrderBy(a => a.letter).ThenByDescending(a => a.result).ToArray(); // Sort the array
foreach(MyClass t in myArray.Where(a => a.letter == 'a')) // The foreach part
{
if (t.result < i && t.test == false)
{
searchResult = index;
break;
}
index++;
}
// And finally write the resulting index [If the element was found]
int计数器=5;//数组的大小
int i=100;//过滤结果的限制
int searchResult=-1;//结果的索引[如果存在]
int index=0;//数组中的索引
MyClass[]myArray=新的MyClass[计数器];//定义数组并填充它
myArray[0]=newMyClass{letter='f',result=12.3,test=false};
myArray[1]=newMyClass{letter='a',result=102.3,test=true};
myArray[2]=newMyClass{letter='a',result=12.3,test=false};
myArray[3]=newMyClass{letter='b',result=88,test=true};
myArray[4]=newMyClass{letter='q',result=234,test=false};
myArray=myArray.OrderBy(a=>a.letter)。然后按降序(a=>a.result)。ToArray();//对数组进行排序
foreach(myArray.Where(a=>a.letter=='a')中的MyClass t)//foreach部分
{
如果(t.result
- 请注意:当然,结果索引将是排序数组中的索引
string
或contain expression,则应该相等如果需要索引,则应该为而不是foreac执行