繁体   English   中英

C#LINQ将扩展翻译为查询语法

[英]C# LINQ Translate Extension to Query syntax

如何将以下LINQ查询语法转换为具有Lambda表达式的扩展语法? 我正在尝试从Galileo计算本开放书C#2012(第11.3.9章)中学习。 以下是使用的类:

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-expressions中编写的代码:

 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,但我不知道如何在没有任何“ from”的情况下编写此代码。如果可以告诉我,那将很好。

两种查询均转换为:

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)
                                    });

你应该可以做这样的事情

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语句。 希望能帮助到你

使用以下代码:

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) });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM