c# - How to get top 3 elements in int array using LINQ? -
I think its a simple LINQ question, but I am very new to LINQ
I have this array:
int [] array = new int [7] (1, 3, 5, 2, 8, 6, 4};
And I have written this code to get the top 3 elements in this array:
var topthree = (I am inferred by selecting the order of the array from me). (3) Take;
And when I find out what's inside the top three:
{System.Linq.Enumerable.TakeIterator}
Nutrition: 0
What did I do, and how to fix my code.
How do you check "What's inside" the top three? The easiest way to do this is to print them:
Using the system; Using System.Linq; Public Class Test {static zero main () {int [] array = new int [7] (1, 3, 5, 2, 8, 6, 4}; var topThree = (I select descending I in the order of the array). Take (3); Forex Currency (Top Three in the top x) {Console.WriteLine (x); }}}
I'm feeling OK ...
Sorting is probably a more efficient way of finding top N values, but it certainly Will work. You may want to consider using dot nation for a query that only works one:
var topThree = array.OrderByDescending (i => i). (3);
Comments
Post a Comment