获取所有匹配值的指标,从使用LINQ名单 [英] Get indexes of all matching values from list using Linq

查看:114
本文介绍了获取所有匹配值的指标,从使用LINQ名单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿LINQ的专家那里,

Hey Linq experts out there,

我刚刚问了一个非常类似的问题,并知道解决的办法可能是超级容易,但还是觉得自己不能够环绕如何使用LINQ做到这一点相当简单的任务,以最有效的方式我的头。

I just asked a very similar question and know the solution is probably SUPER easy, but still find myself not being able to wrap my head around how to do this fairly simple task in the most efficient manner using linq.

我的基本情况是,我有值的列表,例如,说:

My basic scenario is that I have a list of values, for example, say:

Lst1:
a
a
b
b
c
b
a
c
a

和我想创建一个新的列表,将持有的所有指标从LST1的地方,比方说,值=A。 所以,在这个例子中,我们将有:

And I want to create a new list that will hold all the indexes from Lst1 where, say, the value = "a". So, in this example, we would have:

LstIndexes:
0
1
6
8

现在,我知道我可以循环(我宁愿避免赞成的LINQ)做到这一点,我甚至想出如何做到这一点使用LINQ以下列方式:

Now, I know I can do this with Loops (which I would rather avoid in favor of Linq) and I even figured out how to do this with Linq in the following way:

LstIndexes= Lst1.Select(Function(item As String, index As Integer) index) _
                .Where(Function(index As Integer) Lst1(index) = "a").ToList

我这样的挑战是,它遍历列表两倍,因此是无效的。

My challenge with this is that it iterates over the list twice and is therefore inefficient.

我怎样才能让我的成绩在使用LINQ的最有效方法是什么?

How can I get my result in the most efficient way using Linq?

谢谢!!!!

推荐答案

首先,你的code实际上并没有遍历列表两次,只迭代一次。

First off, your code doesn't actually iterate over the list twice, it only iterates it once.

这是说,你的选择其实只是让所有索引的顺序;这是更容易与做 Enumerable.Range

That said, your Select is really just getting a sequence of all of the indexes; that is more easily done with Enumerable.Range:

var result = Enumerable.Range(0, lst1.Count)
             .Where(i => lst1[i] == "a")
             .ToList();

理解为什么这个列表实际上不重复两次将需要一些时间来适应。我会尽力给一个基本解释。

Understanding why the list isn't actually iterated twice will take some getting used to. I'll try to give a basic explanation.

您应该考虑大多数的LINQ方法,如选择和地点作为管道。每种方法做一些工作的点点。在选择的情况下你给它一个方法,它本质上说,每当有人问我为我的下一个项目,我会先问我输入序列的一个项目,然后使用方法我必须把它转换成别的东西,然后给该项目无论是谁用我的。 其中,,或多或少,是在说,每当有人问我一个项目,我会问我的输入序列中的项目,如果函数说,这是很好的我会通过它,如果不是我会继续寻求项目,直到我得到一个通行证。

You should think of most of the LINQ methods, such as Select and Where as a pipeline. Each method does some tiny bit of work. In the case of Select you give it a method, and it essentially says, "Whenever someone asks me for my next item I'll first ask my input sequence for an item, then use the method I have to convert it into something else, and then give that item to whoever is using me." Where, more or less, is saying, "whenever someone asks me for an item I'll ask my input sequence for an item, if the function say it's good I'll pass it on, if not I'll keep asking for items until I get one that passes."

所以,当你把它们连发生的事情是了ToList 询问的第一个项目,它进入其中,来作为它为它的第一个项目,其中,进入选择,并要求它为它的第一个项目,选择进入该列表要求它为它的第一个项目。列表中,则提供了它的第一个项目。 选择然后转换该项目成什么样,它需要吐出(在这种情况下,仅仅是INT 0),并把它交给其中,其中,接受该项目并运行它的功能,它决定的,这是真的,因此吐出 0 了ToList ,这将其添加到列表中。这整个事情再发生了9次。这意味着选择将最终要求从列表中每个项目只出现一次,它会互相喂直接的结果其中,,这将养活结果说,通过测试,直接到了ToList,它们存储在列表中。所有的LINQ方法经过精心设计,永远只迭代源序列一次(当他们被重复一次)。

So when you chain them what happens is ToList asks for the first item, it goes to Where to as it for it's first item, Where goes to Select and asks it for it's first item, Select goes to the list to ask it for its first item. The list then provides it's first item. Select then transforms that item into what it needs to spit out (in this case, just the int 0) and gives it to Where. Where takes that item and runs it's function which determine's that it's true and so spits out 0 to ToList, which adds it to the list. That whole thing then happens 9 more times. This means that Select will end up asking for each item from the list exactly once, and it will feed each of its results directly to Where, which will feed the results that "pass the test" directly to ToList, which stores them in a list. All of the LINQ methods are carefully designed to only ever iterate the source sequence once (when they are iterated once).

请注意,尽管这看起来很复杂,在第一次给你,它实际上是pretty的容易为电脑做这一切。这不是真正的性能降低,因为它似乎在第一。

Note that, while this seems complicated at first to you, it's actually pretty easy for the computer to do all of this. It's not actually as performance intensive as it may seem at first.

这篇关于获取所有匹配值的指标,从使用LINQ名单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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