Linq Reverse is to leave the variable changed or make a copy

I have dicionary strings and dates

private readonly Dictionary<string, DateTime>

as a private member variable in a class

I have methods that recursively get the next and previous value in a dictionary with a given key

eg. It takes the given key, searches for the database, if it is null, it gets the next, etc. Recursively. He does it both forward and backward.

I used this to get the following Find the next entry in the set: LINQ

My question is: can I just add Reverse to the linq statement to do this backwards?

Obviously this will work, but I'm worried about the state in which it will leave a list of member variables in

i.e. will it be canceled? or was a copy made in reverse?

thank

+3
3

Enumerable.Reverse , .

+7

, Enumerable.Reverse ( , ).

, , List.Reverse, .

List<int> list = /* initialize */;
IEnumerable<int> asEnumerable = list;

// these call List<int>.Reverse(), which changes the list
list.Reverse();
((List<int>)asEnumerable).Reverse();

// these call Enumerable.Reverse<int>(), which does not change the list, but returns a new IEnumerable<int>
asEnumerable.Reverse();
((IEnumerable<int>)list).Reverse();
((IList<int>)list).Reverse();

, , .

+3

Linq, Reverse, , , UNLESS ( , : , - Enumerable ).

What makes the inverse probability, since it requires knowledge of the collection's power, is to list the enumerated elements in an array, which it will iterate over in the reverse order. Many of the commonly used Enqerable Linq methods, such as OrderBy, do this behind the scenes.

+1
source

Source: https://habr.com/ru/post/1781669/


All Articles