简体   繁体   English

使用Lambda表达式操作数组的索引

[英]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). 我正在阅读有关LINQ的一些教程,碰到了以下代码,尽管我知道Author正在尝试获取所有第三个元素,但我无法理解这里的'n'在做什么。它清楚地表明我缺乏对Lambda表达式的了解。(如果有人可以为初学者提供掌握链接的方法,那就太好了,截至目前,当我尝试找到它们时,我从来没有发现它们具有扎实的基础知识,而结果是对理解的拙劣理解。 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. 使用Where将本质上循环遍历数组并返回满足给定条件的元素。

n represents the element itself and index represents the index of the element on each iteration . n代表元素本身, index代表每次迭代时元素的索引

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. 因此,where语句逐个遍历数组的每个元素,并每次测试该元素的索引是否可被3整除。

The msdn Lambda article is a good start to gain a better understanding of Lambdas. msdn Lambda文章是一个更好地了解Lambda的良好开始。

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

This line is creating a lambda expression of n and index. 这行代码正在创建n和index的lambda表达式。 n represents the value of the double and index represents the index of that double in the array ExchangedPrices. n表示double的值,而index表示ExchangedPrices数组中该double的索引。 You could use n inside of your lambda expression just like you use index. 您可以在lambda表达式内使用n,就像使用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. 此行将为您提供数组中所有可被3整除的值,而不是按索引对数组中的每个第3个元素进行除数。

Here n represents each item in your ExchangedPrices array. 这里n代表ExchangedPrices数组中的每个项目。 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 . index是一个变量,它保存序列的(从零开始的)索引值,并使用它来检查条件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. 因此,当代码运行时,将在迭代中的每个项目上再次评估此条件,并将返回true或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. LINQ Where子句使用该谓词,并最终根据此if条件的结果返回原始数据的子集。 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. 因此,如果谓词表达式返回true,则将使用相应的项(该迭代时n值)来构建将返回的子集数据。

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. 由于if条件表达式将在循环中评估37个项目时返回12次true ,因此它将从原始数组中获得这12个项目并将其存储到您的yenQuery变量中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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