数量总和列使用LINQ(两个表)

时间:2010-12-26 12:49:44

标签: c# c#-4.0 linq-to-entities group-by

我有两个表可以简单地说:

产品:ProductID,ProductName

订单:OrderID,ProductID,金额,状态

我想用C#写一个LINQ查询      选择ProductName,Sum(Amount),其中Status = 1

我坚持这个简单的查询:(

2 个答案:

答案 0 :(得分:2)

class Program
{
  class Product
  {
     public int ProductID { get; set; }
     public string ProductName {get; set; }
     public Product(int ProductID, string ProductName)
     {
        this.ProductID = ProductID;
        this.ProductName = ProductName;
     }
  }

  class Order
  {
     public int OrderID { get; set; }
     public int ProductID { get; set; }
     public decimal Amount { get; set; }
     public int Status { get; set; }
     public Order(int OrderID, Product product, decimal Amount, int Status)
     {
        this.OrderID = OrderID;
        this.ProductID = product.ProductID;
        this.Amount = Amount;
        this.Status = Status;
     }
  }

  private static Product[] Products;

  private static Order[] Orders;

  static void Main(string[] args)
  {
     Products = new Product[]
     {
        new Product(1, "Bolt"),
        new Product(2, "Nut"),
        new Product(3, "Mounting Plate A"),
        new Product(4, "Mounting Plate B")
     }; 

     Orders = new Order[]
     {
        new Order(1, Products[0], 1.12M, 0),
        new Order(2, Products[1], 0.66M, 1),
        new Order(3, Products[2], 4.12M, 0),
        new Order(4, Products[0], 1.11M, 1),
        new Order(5, Products[1], 0.67M, 1)
     };

     var results = from p in Products
                   join o in Orders on p.ProductID equals o.ProductID
                   where o.Status == 1
                   group o by p
                      into orderTotals
                      select new {ProductName = orderTotals.Key.ProductName, TotalAmount = orderTotals.Sum(o => o.Amount)};

     foreach(var result in results)
     {
        Console.WriteLine("{0}: {1}", result.ProductName, result.TotalAmount);
     }
  }
}

输出:

Bolt: 1.11
Nut: 1.33

答案 1 :(得分:1)

虽然不是最佳,但这应该有效:

var interim =
         from o in orders
         where o.Status == 1
         select new { o.OrderID, o.ProductID, o.Amount, o.Status};

var final =
         from p in products 
         join x in interim on p.ProductID equals x.ProductID into g
         select new { p.ProductName, Total = g.Sum(y => y.Amount) };

HTH!