Linq和Array并在数组中找到nr [英] Linq and Array and find nr in array

查看:65
本文介绍了Linq和Array并在数组中找到nr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友们,



我有一个字符串[]数组,1行符合选择标准。

我可以得到那一行与linq。但我想知道阵列中的哪个亚麻布。



我试过:

Dear friends,

I have a string[] array and 1 line meets a selection criterium.
I can get that line with linq. But I want to know which linenr in the array it is.

I tried:

string[] stringSeparators = new string[] { "\n" };
               string[] lines = completeText.Split(stringSeparators, StringSplitOptions.None);
               var line = lines.Where(x => x.Contains("into @"));
               var lineNr = lines.Select(x => x.IndexOf("into @"));



lineNr nou说数组中的每一行,如果它找到所需的字符串部分@ into。

我希望lineNr将值6保持为数组中具有'@ into'的行的nr。



我该怎么做?


lineNr nou says per line in the array if it ca find the desired stringpart @ into.
I want lineNr to hold value 6 as being the nr in the array that has the line with '@ into'.

How can I do that?

推荐答案

你试过Array.FindIndex [ ^ ] ?


另一个选项是创建自定义索引;)



看看例子:

Abother option is to create custom index ;)

Have a look at example:
string completeText = "a\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk\nl";
string[] stringSeparators = new string[] { "\n" };
var lines = completeText.Split(stringSeparators, StringSplitOptions.None).Select((i, j)=>new{Text = i, LineNo=j});
var lineNr = lines.Where(x=>x.Text.Contains("f")).Select(x=>x.LineNo).FirstOrDefault();





存储数据如下:





lines stores data as follow:

Text LineNo
a    0
b    1
c    2
d    3
e    4
f    5
g    6
h    7
i    8
j    9
k    10
l    11





LineNr 返回: 5


这篇关于Linq和Array并在数组中找到nr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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