linq获取字符串数组的索引 [英] linq to fetch index of string array
问题描述
var _Col_Name_lst =(来自_hdr [0]中的DataColumn x .Table.Columns
选择x.ColumnName)。ToArray();
_Col_Name_lst -----这有一个字符串数组的集合
喜欢
0 - EMl
1 - AZEER
2 - VISTA
--------
在上面的集合中,我需要EMI或者AZEER或VISTA的数组索引,无论我传递给哪一个linq。
所以在上面的集合中使用linq ---_Col_Name_lst
i需要任何索引值EMI或者AZEER或VISTA,无论我通过哪个
尝试下面的代码
var index = 0 ;
var _Col_Name_lst =( from DataColumn x in _hdr [ 0 ]。Table.Columns
select new {
RowIndex = index ++,
ColumnName = x.ColumnName}。)ToArray();
在新列表中RowIndex
将包含每个列名的索引。
var result = _Col_Name_lst.Select((s,i)= > new {Pos = i,text = s})
.Where(currentItem = > currentItem.text .ToUpper()== AZEER) // 仅选择匹配项
。选择(n = > n.Pos); // 只选择你想要的,即只是项目的位置
注意:这里的结果只会存储匹配项的索引值
var _Col_Name_lst = (from DataColumn x in _hdr[0].Table.Columns
select x.ColumnName).ToArray();
_Col_Name_lst ----- This has a collection of string array
like
0 - EMl
1 - AZEER
2 - VISTA
--------
Here in above collection, i require array index of EMI or AZEER or VISTA whichever i pass to a linq.
so using linq on above collection --- "_Col_Name_lst "
i require index of any values EMI or AZEER or VISTA whichever i pass
try below code
var index=0; var _Col_Name_lst = (from DataColumn x in _hdr[0].Table.Columns select new { RowIndex = index++, ColumnName = x.ColumnName}).ToArray();
In the new listRowIndex
will contain the index of each column name.
var result = _Col_Name_lst.Select((s, i) => new { Pos = i, text =s}) .Where(currentItem => currentItem.text.ToUpper()=="AZEER") //select matching item only .Select(n=> n.Pos);// select only what you want, i.e. just position of item
Note: here result will just store index value of matching item
这篇关于linq获取字符串数组的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!