简体   繁体   中英

C# LINQ Translate Extension to Query syntax

How can I translate the following LINQ query syntax into an extension syntax with Lambda-expressions? I am trying to learn from galileo computing open book C#2012 on chapter 11.3.9. Here are the classes used:

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

In customerList there are some "Customer" objects and in the property Orders you can find a list of "Order" objects. Here is the code I want to write in 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) };

I think I should use GroupJoin but I can't figure out how to write this without any "from" It would be nice if you could show me.

Both queries translated:

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

You should be able to do something like this

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

and a similar linq statement for the second. Hope it helps

Use Following code:

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

and

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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