Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get indexes of all matching values from list using Linq

Tags:

c#

.net

vb.net

linq

Hey Linq experts out there,

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

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

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.

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

Thanks!!!!

like image 713
John Bustos Avatar asked Oct 24 '12 18:10

John Bustos


3 Answers

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

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.

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."

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).

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.

like image 55
Servy Avatar answered Oct 16 '22 05:10

Servy


This works, but arguably not as neat.

var result = list1.Select((x, i) => new {x, i})
                  .Where(x => x.x == "a")
                  .Select(x => x.i);
like image 8
Jim Johnson Avatar answered Oct 16 '22 04:10

Jim Johnson


How about this one, it works pretty fine for me.

   static void Main(string[] args)
    {
        List<char> Lst1 = new List<char>();
        Lst1.Add('a'); 
        Lst1.Add('a');   
        Lst1.Add('b');   
        Lst1.Add('b');   
        Lst1.Add('c');   
        Lst1.Add('b');   
        Lst1.Add('a');   
        Lst1.Add('c');
        Lst1.Add('a');

        var result = Lst1.Select((c, i) => new { character = c, index = i })
                         .Where(list => list.character == 'a')
                         .ToList();
    }
like image 1
luis_laurent Avatar answered Oct 16 '22 06:10

luis_laurent