Linq从数组列表中获取索引 [英] Linq getting index from the arraylist
问题描述
你好,
我已经编写了一些代码来使用linq获取该项目的索引,但是它无法正常工作.
请检查一下,并在可能的情况下告诉您什么地方
Hi there
I have written some code to get index of the item using linq but it is not working properly.
Please check it out and if possible tell what is wrong in here
var df= (from c in clsLibraryMapping.libData
where c.Value.strFunctionName == itemEffect.strECMType
select c.Value.arlPName);
var dsf = df.Select((v, i) => new
{ pname = v, index = i })
.Where(a => a.pname[a.index].ToString() == "Y")
.First().index ;
的数据是
data for this is
sl no fun cm param input
1 d r y y
h h
g j
t g
2 f g g y
h j
y h
i u
在第一个查询中,我通过f并获取第二行param(g,h,y,i)
第二个查询,我想要y的索引.即2,但只有g得到检查,并且控制权返回到第一个查询并检查下一行,因为没有第3行检查它是否抛出错误.
实际上,它检查了参数g,h,y并把它写成3.
所以这是错误的.帮帮我.
in first query i m passing f and getting the second row param(g,h,y,i)
second query i want index of y. ie 2 but only g is getting checked and control is going back to first query and checking for next row as there is no row 3 to check it is throwing error.
actually it shud check in param g,h,y and it shud written 3.
So waht is wrong. Help me.
推荐答案
更改第二个查询,如下所示:
Change your second query as follows:
var dsf = df.Select((v, i) => new
{ pname = v, index = i })
.Where(a => a.pname == "Y")
.First().index ;
还请记住,搜索将区分大小写.因此,在比较时,您可以使用字符串的ToUpper()函数或仅与实际数据中存在的文本进行比较.
问候
Praveen
Also remember here searching will be case sensitive. So while comparing you can use ToUpper() function of string or simply compare with text as it is present in the actual data.
Regards
Praveen
这篇关于Linq从数组列表中获取索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!