MSDN orderby clause
var people = from x in excel.Worksheet<CountryEconomics>("Sheet1")
let c = x.Inflation/x.GDP
orderby c
select c;
我不能只是一個數組複製:
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));
這可能是與LINQ到Excel中的一個漏洞。 (我不能測試這個。)
如果是這樣的話,你可以強制評估(通過下面的.ToArray()
)然後對其進行排序。作爲LINQ的任何靜態數據的消費者,我會預計ToArray
調用是不必要的。
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));
那麼問題是什麼?在什麼地方卡住了? –
_我無法正確理解_你得到了什麼輸出,你期望什麼? –
我無法按升序排列,對不起。 – marseilles84