【问题标题】:Entity Framework Linq, Left Join and group with SUM and CountEntity Framework Linq, Left Join and group with SUM and Count
【发布时间】:2017-10-26 00:22:35
【问题描述】:

在将 SQL 转换为 Linq 时,我需要一些帮助。在 MySQL 中这很简单......

Table: customers
ID  Name    
1   Bill
2   John

Table: purchases
ID  CustomerID  CompletedTransaction
1   1           False
2   2           True
3   1           True
4   1           True


    SELECT  c.ID
        c.Name,
        COUNT(p.ID) AS TotalPurchases,
        SUM(CASE WHEN p.CompletedTransaction = TRUE THEN 1 ELSE 0 END) AS                       TotalCompleted
    FROM customers c
    LEFT JOIN purchases p ON c.ID = p.CustomerID
    GROUP BY c.ID

Expected Result:
1, Bill, 3, 2
2, John, 1, 1

我已经看到了一些关于如何在 Linq 中实现左连接的示例,但我不确定如何在其中包含 SUM 和 Count。我在 Linq 中看到了从组键中选择返回的字段的示例。这是否意味着如果我在客户表中有更多字段,例如我想返回的地址和其他联系方式,我必须将它们包含在连接中然后才能选择它们?希望这是有道理的。感谢任何可能为我指明正确方向的帮助或链接。

谢谢

【问题讨论】:

    标签: c# mysql entity-framework linq entity


    【解决方案1】:
    var answer = (from c in db.customers 
                  join p in db.purchases 
                  on c.ID = p.CustomerID into subs
                  from sub in subs.DefaultIfEmpty()
                  group sub by new { c.ID, c.Name } into gr
                  select new {
                      gr.Key.ID,
                      gr.Key.Name,
                      Total = gr.Count(x => x != null),
                      CountCompleted = gr.Count(x => x != null && x.CompletedTransaction)
                  }).ToList();
    

    【讨论】:

      【解决方案2】:

      这是示例

      class Program
          {
              static void Main(string[] args)
              {
      
                  List<Customers> customers = new List<Customers>();
                  customers.Add(new Customers() { ID = 1, Name = "Bill" });
                  customers.Add(new Customers() { ID = 2, Name = "John" });
      
                  List<Purchases> purchases = new List<Purchases>();
                  purchases.Add(new Purchases() { ID = 1, CustomerID = 1, CompletedTransaction = false });
                  purchases.Add(new Purchases() { ID = 2, CustomerID = 2, CompletedTransaction = true });
                  purchases.Add(new Purchases() { ID = 3, CustomerID = 1, CompletedTransaction = true });
                  purchases.Add(new Purchases() { ID = 4, CustomerID = 1, CompletedTransaction = true });
      
                  IEnumerable<JoinResult> results = from c in customers
                                             join p in purchases
                                             on c.ID equals p.CustomerID
                                             group new { c, p } by new {p.CustomerID, c.Name} into r
                                             select new JoinResult
                                             {
                                                 CustomerID = r.Key.CustomerID,
                                                 CustomerName = r.Key.Name,
                                                 TotalPurchases = r.Count(),
                                                 TotalCompleteTransaction = r.Where(s=> s.p.CompletedTransaction).Count()
                                             };
      
                  foreach(JoinResult r in results)
                  {
                      Console.WriteLine($"CustomerID : {r.CustomerID} | Name : {r.CustomerName} | TotalPurchases : {r.TotalPurchases} | TotalCompleteTransaction : {r.TotalCompleteTransaction}");
                  }
      
                  Console.ReadKey();
              }
          }
      
          class Customers
          {
              public int ID { get; set; }
              public string Name { get; set; }
          }
      
          class Purchases
          {
              public int ID { get; set; }
              public int CustomerID { get; set; }
              public bool CompletedTransaction { get; set; }
          }
      
          class JoinResult
          {
              public int CustomerID { get; set; }
              public string CustomerName { get; set; }
              public int TotalPurchases { get; set; }
              public int TotalCompleteTransaction { get; set; }
          }
      

      结果

      【讨论】:

      • 谢谢你,虽然看起来这给出了内连接而不是左连接。
      猜你喜欢
      • 2010-12-13
      • 1970-01-01
      • 2017-05-29
      • 1970-01-01
      • 1970-01-01
      • 2017-12-07
      • 2021-08-02
      • 1970-01-01
      • 2023-03-04
      相关资源
      最近更新 更多