Question

I have this data, and I'm using linqToExcel: enter image description here

I'm trying to get inflation divided by GDP...then order them ascending, but I can't get it right.

    var people = from x in excel.Worksheet<CountryEconomics>("Sheet1")
                 let c = x.Inflation / x.GDP
                 orderby c ascending 
                 select c;

I'm getting the output:

12
6
4
3
2
2

no matter if I put ascending or descending in the query. How can I get the data ascending? i.e.

2
2
3
4
6
12
Was it helpful?

Solution 3

Right now, I'm just guessing, but maybe adding some casts will make it work:

var people = from x in excel.Worksheet<CountryEconomics>("Sheet1")
             let c = ((double)x.Inflation) / ((double)x.GDP)
             orderby c ascending 
             select c;

However if that fails as well - what'll happen if you make it a list first:

var people = from x in excel.Worksheet<CountryEconomics>("Sheet1").ToList()
             let c = ((double)x.Inflation) / ((double)x.GDP)
             orderby c ascending
             select c;

If that's still failing:

var people = from x in excel.Worksheet<CountryEconomics>("Sheet1")
             let c = ((double)x.Inflation) / ((double)x.GDP)
             select c;

var peopleList = people.ToList().OrderBy(p => p);

Hope this gets it done...

OTHER TIPS

MSDN orderby clause

var people = from x in excel.Worksheet<CountryEconomics>("Sheet1")
             let c = x.Inflation / x.GDP
             orderby c
             select c;  

I can't reproduce with just an array:

var economics = new[]
    {
        new {Country = "USA", GDP = 1, Inflation = 12},
        new {Country = "GB", GDP = 2, Inflation = 12},
        new {Country = "JPN", GDP = 3, Inflation = 12},
        new {Country = "GER", GDP = 4, Inflation = 12},
        new {Country = "CHI", GDP = 5, Inflation = 12},
        new {Country = "CAN", GDP = 6, Inflation = 12},
    };

var people = from x in economics
             let c = x.Inflation/x.GDP
             orderby c
             select c;

// without "orderby c":  12, 6, 4, 3, 2, 2
// with "orderby c":  2, 2, 3, 4, 6, 12
Console.WriteLine(string.Join(", ", people));

It could be a flaw with Linq-to-Excel. (I'm not in a position to test this.)

If that's the case, you could force the evaluation (via .ToArray() below) and then sort it. As a consumer of any static data with LINQ, I would expect the ToArray call to be unnecessary.

var people = from x in economics
             let c = x.Inflation/x.GDP
             select c;

var sorted = people.ToArray().OrderBy(c => c);
Console.WriteLine(string.Join(", ", sorted));

If all you want is to sort by Inflation / GDP, you can just do this:

var people = from x in excel.Worksheet<CountryEconomics>("Sheet1")
             orderby x.Inflation / x.GDP
             select x;

Or in fluent syntax:

var people = excel.Worksheet<CountryEconomics>("Sheet1")
                  .OrderBy(x => x.Inflation / x.GDP);

I'm not sure, but you may need to skip the first row (which has the headers).

var people = excel.Worksheet<CountryEconomics>("Sheet1")
                  .Skip(1).OrderBy(x => x.Inflation / x.GDP);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top