的我有很多在以下格式的數據..前5和另一列前五名每個第一列
var data1 = new[] {
new { Product = "Product 1", Year = 2009, Sales = 1212 },
new { Product = "Product 2", Year = 2009, Sales = 522 },
new { Product = "Product 1", Year = 2010, Sales = 1337 },
new { Product = "Product 2", Year = 2011, Sales = 711 },
new { Product = "Product 2", Year = 2012, Sales = 2245 },
new { Product = "Product 3", Year = 2012, Sales = 1000 }
};
如果我想獲得前20行,最大的銷售,我可以做一些如下...
data1.OrderByDescending(o=>o.Sales).Take(20);
但我想要做的是獲得前5名產品和(對於那些產品)前5年隨着他們的銷售。
因此,輸出會像下面的東西:
var outputdata = new[] {
new { Product = "Product 1", Year = 2012, Sales = 2245 },
new { Product = "Product 1", Year = 2010, Sales = 1337 },
new { Product = "Product 1", Year = 2009, Sales = 1212 },
new { Product = "Product 1", Year = 2011, Sales = 711 },
new { Product = "Product 1", Year = 2013, Sales = 522 },
new { Product = "Product 2", Year = 2012, Sales = 1000 }
};
This可能是SQL類似的問題。但不幸的是無法理解如何轉換爲LINQ。
的你能不能顯示輸出的例子嗎? –
輸出結果與輸入相同 - {Product =「Product 1」,Year = 2009,Sales = 1212},如果您的問題是其他內容,請再次詢問,謝謝 – Arnab
我的意思是可以用一個例子來說明想要的輸出? –