Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 复杂Linq查询查找另一个集合中n个项的字段总和_C#_Linq - Fatal编程技术网

C# 复杂Linq查询查找另一个集合中n个项的字段总和

C# 复杂Linq查询查找另一个集合中n个项的字段总和,c#,linq,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; } pub

我有一些样本数据,正在试图确定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 }};
现在,对于这5个要素中的每一个,我需要了解1号客户在2013年第6个月购买了多少物品。我必须确定6月份发放了哪些收件人,然后检查相关的详细信息是否包括前5项中的一项


我试着做一个Linq表达式。但是可以选择多个表达式。

下面是一个带有第二个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

但是人群在那里唱着一行!如果愿意,您可以在一行上生成查询语法。我认为它大约和方法语法一样长,但大约有350个字符,所以大约有350个字符。这对我来说太长了。
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