2016-10-21 37 views
0

的我有很多在以下格式的数据..前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。

+0

的你能不能显示输出的例子吗? –

+0

输出结果与输入相同 - {Product =“Product 1”,Year = 2009,Sales = 1212},如果您的问题是其他内容,请再次询问,谢谢 – Arnab

+0

我的意思是可以用一个例子来说明想要的输出? –

回答

1

好的,如果我理解正确:第一组由product,所以你可以订购的产品总数sales。 然后你可以只采取你想要的金额。使用SelectMany扁平化组:

var data = 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 } 
     }; 
int numberOfProducts = 2; 
int numberOfYearsForEachProduct = 3; 

var result = data.GroupBy(x => x.Product) 
    .OrderByDescending(x => x.Sum(y => y.Sales)) //Order products by their total sum of `Sales` 
    .Take(numberOfProducts) 
    .SelectMany(x => x.OrderByDescending(y => y.Sales).Take(numberOfYearsForEachProduct)) // Take only the N top years for each product 
    .ToList(); 

我在Take使用更小的数字,所以我可以看到它正在做正确

+0

是的,我想下一个订购也在销售.. – Arnab

+1

@Arnab - 更新了答案。很高兴帮助你 –

0

如果我让你正确的,你想获得前20名的销售为前5名产品。

var ord = data1.OrderByDescending(o => o.Sales) 
       .Select(o => o.Product) 
       .Distinct().Take(5);//Get top 5 products by their sales 

var salesForTopProducts = data1.OrderByDescending(o => o.Sales) 
           .Where(o => ord.Contains(o.Product)) 
           .Take(20);//Get top 20 sales for top 5 products 
1

首先,你应该得到的20个最畅销的产品

var top20Products = data1 
    .GroupBy(x => x.Product) 
    .OrderByDescending(group => group.Sum(x => x.Sales)) 
    .Select(group => group.Key) 
    .Take(20); 

,然后选择前5个最畅销的年他们

var top5yearsOfTop20products = top20Products 
    .SelectMany(product => data1 
     .Where(x => x.Product == product) 
     .OrderByDescending(x => x.Sales) 
     .Take(5)); 
相关问题