Complex Linq Query查找另一个集合中n个项的字段总和

时间:2013-10-30 18:05:26

标签: c# linq

我有一些样本数据,我正在尝试确定5个“销售最多的产品”,然后尝试查找ID为#1的客户购买了多少这些产品 在2013年6月。

我有以下数据结构:

public class MonthlyHistoric
{

  public int StoreCode { get; set; }
  public int Month { get; set; }
  public int Year { get; set; }
  public int NumberOfSales { get; set; }
  public int ItemCode { get; set; }

}

public class Recipt
{
  public int Id { get; set; }
  public DateTime SaleDate { get; set; }
  public int CustomerID {get;set;}
}

public class ReciptDetails
{
  public int Id { get; set; }
  public int IdRecipt { get; set; }
  public int ItemCode { get; set; }
  public int AmountSold { get; set; }
}

我有以下测试数据:

List<MonthlyHistoric> Historic = new List<MonthlyHistoric> { 
  new MonthlyHistoric() {ItemCode= 101, Month=6, NumberOfSales=20,StoreCode=7,Year= 2013 },
  new MonthlyHistoric() {ItemCode= 102, Month=6, NumberOfSales=100,StoreCode=7,Year= 2013 },
  new MonthlyHistoric() {ItemCode= 103, Month=6, NumberOfSales=30,StoreCode=7,Year= 2013 },
  new MonthlyHistoric() {ItemCode= 105, Month=6, NumberOfSales=20,StoreCode=3,Year= 2013 }};

List<Recipt> Recipts = new List<Recipt> { 
  new Recipt() { Id = 1, SaleDate = new DateTime(2013, 6, 02), CustomerID =1 },
  new Recipt() { Id = 2, SaleDate = new DateTime(2013, 7, 01), CustomerID =1 },
  new Recipt() { Id = 3, SaleDate = new DateTime(2013, 6, 04), CustomerID =2 },
  new Recipt() { Id = 4, SaleDate = new DateTime(2013, 5, 14), CustomerID =1 },
  new Recipt() { Id = 5, SaleDate = new DateTime(2013, 6, 16), CustomerID =2 },
  new Recipt() { Id = 6, SaleDate = new DateTime(2013, 6, 12), CustomerID =1 }};

List<ReciptDetails> Details = new List<ReciptDetails> { 
   new ReciptDetails() { AmountSold = 200, Id = 1, IdRecipt = 1, ItemCode = 101 },
   new ReciptDetails() { AmountSold = 234, Id = 1, IdRecipt = 1, ItemCode = 101 },
   new ReciptDetails() { AmountSold = 2050, Id = 1, IdRecipt = 2, ItemCode = 101 },
   new ReciptDetails() { AmountSold = 20340, Id = 1, IdRecipt = 2, ItemCode = 102 },
   new ReciptDetails() { AmountSold = 2300, Id = 1, IdRecipt = 3, ItemCode = 102 },
   new ReciptDetails() { AmountSold = 2200, Id = 1, IdRecipt = 3, ItemCode = 103 },
   new ReciptDetails() { AmountSold = 200, Id = 1, IdRecipt = 4, ItemCode = 101 },
   new ReciptDetails() { AmountSold = 10, Id = 1, IdRecipt = 5, ItemCode = 101 },
   new ReciptDetails() { AmountSold = 2400, Id = 1, IdRecipt = 6, ItemCode = 101 }};

使用以下表达式,我可以获得2013年第6个月商店销售的前5个产品(本例中为#7)

Historic.Where (x => x.StoreCode == 7 && x.Year == 2013 && x.Month== 6).OrderBy (x =>x.NumberOfSales ).Take(5)

现在,对于这5个元素中的每一个,我需要找出客户1号在2013年第6个月购买了多少件物品。我必须确定6月份发出的收件人数量,然后检查相关的详细信息是否包含一个前5项。

我正在尝试做一个单一的Linq表达式。但是多个表达式是一种选择。

1 个答案:

答案 0 :(得分:0)

这是第二个LINQ表达式的示例。我还将你的一条长行重写为(IMHO)更易读的查询语法。

var topItems = (from h in Historic
                where h.StoreCode == 7 && h.Year == 2013 && h.Month == 6
                orderby h.NumberOfSales
                select h.ItemCode).Take(5).ToList();
var sum =
   (from r in Recipts
    where r.CustomerID == 1 && r.SaleDate.Year == 2013 && r.SaleDate.Month == 6
    join d in Details on r.Id equals d.IdRecipt
    where topItems.Contains(d.ItemCode)
    select d.AmountSold).Sum();
Console.WriteLine(sum); // 2834