Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reversely query the List in C# LINQ

Tags:

c#

linq

List<List<double>> Return(List<double> vector, int Z, int firstidx)
{            
    return vector.Reverse()
                 .Skip(firstidx)
                 .Take(Z)
                 .Select(i => vector.Reverse().Select(j => j != 0? i / j : 0.0).ToList())
                 .ToList();
}

I want to reversely query the List but there is some error in the .Reverse() and it said that:

Operator '.' cannot be applied oprand of type 'void'`.

Even I create a intermediate variable List<double> Reversevector = vector.Reverse().ToList();

So what the correct way to use .Reverse() in linq?

like image 295
user6703592 Avatar asked Dec 18 '22 14:12

user6703592


1 Answers

Problem is you are using List.Reverse() method not the Enumerable.Reverse()

you have two options, either to call it as static method or explicit casting.

Enumerable.Reverse(vector)
          .Skip(firstidx)
           .Take(Z)
           .Select(i => Enumerable.Reverse(vector).Select(j => j != 0? i / j : 0.0).ToList())
           .ToList(); 
like image 88
Hari Prasad Avatar answered Dec 24 '22 01:12

Hari Prasad