的我有很多在以下格式的数据..前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
我的意思是可以用一个例子来说明想要的输出? –