C#LINQ将扩展转换为查询语法

时间:2014-01-15 09:14:57

标签: c# linq lambda

如何将以下LINQ查询语法转换为具有Lambda表达式的扩展语法?我试图从第11.3.9章中的galileo cumputing open book C#2012中学习。 以下是使用的类:

public class Customer
{   public string Name { get; set; }
    public List<Order> Orders { get; set; }
}
public class Order
{   public int OrderID { get; set; }
    public int ProductID { get; set; }
    public int Quantity { get; set; }
}
public class Product
{   public int ProductID { get; set; }
    public double Price { get; set; }
}

在customerList中有一些“客户”对象,在属性订单中,您可以找到“订单”对象的列表。 这是我想用lambda表达式编写的代码:

 var allOrders =
       from cust in customerList
       from ord in cust.Orders
       join prod in productList on ord.ProductID equals prod.ProductID
       select new
       {
           cust.Name,
           ord.ProductID,
           OrderAmount = ord.Quantity * prod.Price
       };

 var summe =
       from cust in customerList
       join ord in allOrders
       on cust.Name equals ord.Name into custWithOrd
       select new { cust.Name, TotalSumme = custWithOrd.Sum(s => s.OrderAmount) };

我想我应该使用GroupJoin,但我无法弄清楚如何在没有任何“来自”的情况下编写 如果你能给我看,那就太好了。

3 个答案:

答案 0 :(得分:2)

翻译了两个查询:

var allOrders2 = customerList.SelectMany(cust => cust.Orders, 
                                         (cust, ord) => new
                                         {
                                            cust,
                                            ord
                                         })
                              .Join(productList, 
                                    x => x.ord.ProductID, 
                                    prod => prod.ProductID, 
                                    (x, prod) => new
                                    {
                                        Name = x.cust.Name,
                                        ProductID = x.ord.ProductID,
                                        OrderAmount = (double)x.ord.Quantity * prod.Price
                                    });
var summe2 = customerList.GroupJoin(allOrders, 
                                    cust => cust.Name, 
                                    ord => ord.Name, 
                                    (cust, custWithOrd) => new
                                    {
                                        Name = cust.Name,
                                        TotalSumme = custWithOrd.Sum(s => s.OrderAmount)
                                    });

答案 1 :(得分:1)

使用以下代码:

var ordersByCustomers = customers.Select(x => new { CustomerName = x.Name, Product = x.Orders.Join(products, order => order.ProductID, product => product.ProductID, (order, product) => new { ProductID = product.ProductID, TotalAmount = order.Quantity * product.Price }).Select(y => y) });

var totalAmountByCustomers = ordersByCustomers.Select(x => new { CustomerName = x.CustomerName, TotalAmount = x.Product.Sum(y => y.TotalAmount) });

答案 2 :(得分:0)


你应该可以做这样的事情

var allOrders = customerList.SelectMany(o=>o.Orders.Select(i=>new {Name = o.Name,Quantity =o.Quantity,ProductID=o.ProductID  })).Join(productList,order=>order.ProductID,prod=>prod.ProductID,(order, prod)=> new {...});

和第二个类似的linq语句。希望它有所帮助