Get a column list of values in LINQ

79 Views Asked by At

I would like to get a column of Ages with the values of each row to calculate the Mode. enter image description here

Here is my code:

int[] ages = ?

 var mode = ages.GroupBy(n => n).
           OrderByDescending(g => g.Count()).
           Select(g => g.Key).FirstOrDefault();

My problem is I cant get a lambda expression on grid holding the column of values.

1

There are 1 best solutions below

0
faflo10 On

You can try something like (if we name you table Person, as I don't know its name) :

List<int> listAges = new List<int>();
// Here db is your DataContext.
listAges = db.Person.OrderByDescending(p => p.Age).Select(p => p.Age).ToList();

Now, in the list named listAges, you have all ages from you table ordered by their age (descending). And if you prefer working on an array, use :

int[] ages = listAges.ToArray();