linq获取字符串数组的索引 [英] linq to fetch index of string array

查看:105
本文介绍了linq获取字符串数组的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 list RowIndex 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆