Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.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# 聚合查询上的实体框架左连接_C#_Entity Framework_Entity Framework 6 - Fatal编程技术网

C# 聚合查询上的实体框架左连接

C# 聚合查询上的实体框架左连接,c#,entity-framework,entity-framework-6,C#,Entity Framework,Entity Framework 6,我有一个EF查询,它包含许多聚合子查询(Count、Sum和Max)。有两个问题 我需要将子查询上的连接保留为左连接,这样即使聚合子查询中没有结果,也会返回记录。目前,仅当所有子查询返回记录时才返回记录 返回的批发商CustomerAndAgreegateOrders对象的结果列表包含一个联系人对象,该对象还需要包括地址和县。我在查询中添加了Include(c=>c.Addresses.Select(a=>a.Country)),但是联系人对象不包含任何地址对象 对于任何一个问题的任何帮助都将不

我有一个EF查询,它包含许多聚合子查询(Count、Sum和Max)。有两个问题

  • 我需要将子查询上的连接保留为左连接,这样即使聚合子查询中没有结果,也会返回记录。目前,仅当所有子查询返回记录时才返回记录

  • 返回的批发商CustomerAndAgreegateOrders对象的结果列表包含一个联系人对象,该对象还需要包括地址和县。我在查询中添加了
    Include(c=>c.Addresses.Select(a=>a.Country))
    ,但是联系人对象不包含任何地址对象

  • 对于任何一个问题的任何帮助都将不胜感激。下面是完整的查询

    var month1Date = DateTime.Today.AddMonths(-1);
    var month3Date = DateTime.Today.AddMonths(-3);
    var month6Date = DateTime.Today.AddMonths(-6);
    var month12Date = DateTime.Today.AddMonths(-12);
    
    var db = GetNewContext();
    var qry = from c in db.Contacts
                          .Include(c => c.Addresses.Select(a => a.Country))
              join orderCount in
                  (
                      from o in db.WholesaleOrders
                      group o by o.ContactId into g
                      select new
                      {
                          ContactId = g.Key,
                          TotalOrders = g.Count()
                      }
                  ) on c.Id equals orderCount.ContactId
              join month1Value in
                  (
                      from o in db.WholesaleOrders
                      where o.OrderDate >= month1Date
                      group o by o.ContactId into g
                      select new
                      {
                          ContactId = g.Key,
                          TotalValue = g.Sum(r => r.LineItems.Sum(l => l.QuantityOrdered * l.Price))
                      }
                  ) on c.Id equals month1Value.ContactId
              join month3Value in
                  (
                      from o in db.WholesaleOrders
                      where o.OrderDate >= month3Date
                      group o by o.ContactId into g
                      select new
                      {
                          ContactId = g.Key,
                          TotalValue = g.Sum(r => r.LineItems.Sum(l => l.QuantityOrdered * l.Price))
                      }
                  ) on c.Id equals month3Value.ContactId
              join month6Value in
                  (
                      from o in db.WholesaleOrders
                      where o.OrderDate >= month6Date
                      group o by o.ContactId into g
                      select new
                      {
                          ContactId = g.Key,
                          TotalValue = g.Sum(r => r.LineItems.Sum(l => l.QuantityOrdered * l.Price))
                      }
                  ) on c.Id equals month6Value.ContactId 
              join month12Value in
                  (
                      from o in db.WholesaleOrders
                      where o.OrderDate >= month12Date
                      group o by o.ContactId into g
                      select new
                      {
                          ContactId = g.Key,
                          TotalValue = g.Sum(r => r.LineItems.Sum(l => l.QuantityOrdered * l.Price))
                      }
                  ) on c.Id equals month12Value.ContactId
              join month12Quantity in
                  (
                      from o in db.WholesaleOrders
                      where o.OrderDate >= month12Date
                      group o by o.ContactId into g
                      select new
                      {
                          ContactId = g.Key,
                          OrderCount = g.Count()
                      }
                  ) on c.Id equals month12Quantity.ContactId
              join lastOrderDate in
                  (
                      from o in db.WholesaleOrders
                      group o by o.ContactId into g
                      select new
                      {
                          ContactId = g.Key,
                          LastOrderDate = g.Max(r => r.OrderDate)
                      }
                  ) on c.Id equals lastOrderDate.ContactId
              select new WholesaleCustomerAndAggregateOrders
              {
                  Contact = c,
                  TotalOrders = orderCount.TotalOrders,
                  Month1Value = month1Value.TotalValue,
                  Month3Value = month3Value.TotalValue,
                  Month6Value = month6Value.TotalValue,
                  Month12Value = month12Value.TotalValue,
                  Month12OrderCount = month12Quantity.OrderCount,
                  LastOrderDate = lastOrderDate.LastOrderDate
              };
    
    return await qry.ToListAsync();
    
    这个怎么样:

    db.WholesaleOrders
        .GroupBy(o => o.ContactId)
        .Select(a => new { 
            a.Key, 
            TotalOrders = a.Count(),
            LastOrderDate = a.Max(r => r.OrderDate),
            Month1Value = a.Where(b => b.OrderDate >= month1Date).Sum(r => r.LineItems.Sum(l => l.QuantityOrdered * l.Price),
            Month3Value = a.Where(b => b.OrderDate >= month3Date).Sum(r => r.LineItems.Sum(l => l.QuantityOrdered * l.Price),
            Month6Value = a.Where(b => b.OrderDate >= month6Date).Sum(r => r.LineItems.Sum(l => l.QuantityOrdered * l.Price),
            Month12Value = a.Where(b => b.OrderDate >= month12Date).Sum(r => r.LineItems.Sum(l => l.QuantityOrdered * l.Price)
            }).ToListAsync();
    
    更新:向投影添加另一个属性:

    Addresses = db.Addresses.Where(ad => ad.ContactId == a.Key);
    
    其中db是您的上下文,ad.ContactId是地址表中联系人的FK


    要使其正常工作,必须将连接的multipleactiveresultsets属性设置为True。

    如果在一个方法中只有一个
    Wait
    ,则不要将
    async
    return Wait
    一起使用,返回任务本身,这确实更有效。您好,谢谢。这解决了聚合问题,这是向前迈出的一大步。但是,Contact仍然不会返回填充的地址集合。为了解决这个问题,我正在执行第二个查询,以提取具有地址和国家/地区的联系人,然后将联系人添加到批发商CustomerAndAggregateOrder对象中。如果有人知道如何做这是一个单一的查询,请分享。