简体   繁体   中英

Manipulating array's index using Lambda Expression

I was going through some tutorial about LINQ and I bumped into following code, I could not understand what 'n' is doing here,though I understand Author is trying to get every third element.It clearly shows I lack understanding of Lambda expression.(It would be great if some one can provide beginner to master link for that, as of now when I try to find them I never find them with solid fundamentals and result is botched understanding). In following array every third element is in 'Yen'(currency).

 static double[] ExchangedPrices = {827.70, 604.50, 111869.70,
                                        1869.00, 1,365.00, 252609.00,
                                        521.36, 380.77, 70465.88,
                                        455.68, 332.80, 61588.48,
                                        2018.34, 1474.07, 272793.66,
                                        920.26, 672.10, 124379.86,
                                        1873.45, 1368.25, 253210.45,
                                        149.34, 109.07, 20184.66,
                                        455.68, 332.80, 61588.48,
                                        525.28, 383.63, 70995.16,
                                        9.08, 6.63, 1226.96,
                                        311.50, 227.50, 42101.50};

 IEnumerable<double> yenQuery = ExchangedPrices.Where((n, index) => index%3 == 0);

Using Where will essentially loop through the array and return elements that meet a given condition.

n represents the element itself and index represents the index of the element on each iteration .

So the where statement is going through each of the elements of the array one by one and each time testing if the index of that element is divisible by 3.

The msdn Lambda article is a good start to gain a better understanding of Lambdas.

ExchangedPrices.Where((n, index) => index%3 == 0);

This line is creating a lambda expression of n and index. n represents the value of the double and index represents the index of that double in the array ExchangedPrices. You could use n inside of your lambda expression just like you use index.

ExchangedPrices.Where((n, index) => n%3 == 0);  

This line would get you all of the values in your array that are divisible by 3 as opposed to every 3rd element in the array by index.

Here n represents each item in your ExchangedPrices array. index is variable which holds the (zero based) index value of the sequence and using that we are checking an if condition index%3==0 . So when the code runs, This condition will be evaluated againist each item in the iteration and will return true or false. The LINQ Where clause takes this predicate and will eventually return a subset of original data based on the result of this if condition. So if the predicate expression returns true, the corresponding item (value of n at that iteration) will be used to build the subset data which will be returned.

Since the if condition expression is going to return true for 12 times while evaluating 37 items in the loop, It will get those 12 items from your original array and will be stored to your yenQuery variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM