2

I have these following classes from an Linq Example:

public class Customer
{
    public Customer();

    public Cities City { get; set; }
    public string Name { get; set; }
    public Order[] Orders { get; set; }
}

public class Product
{
    public Product();

    public double Price { get; set; }
    public int ProductID { get; set; }
    public string ProductName { get; set; }
}

public class Order
{
    public Order();

    public int OrderID { get; set; }
    public int ProductID { get; set; }
    public int Quantity { get; set; }
    public bool Shipped { get; set; }
}

static void Main(string[] args)
{
    var allOrders = from cust in customers
                    from ord in cust.Orders
                    join prod in products on ord.ProductID equals prod.ProductID
                    select new
                    {
                        cust.Name,
                        ord.ProductID,
                        OrderAmount = ord.Quantity * prod.Price
                    };
}

I want to create the same collection (Name, ProductID, Orderamount) with the linq Extension Method Syntax. My problem is that I don't know how to realize the two datasources from cust in customers from ord in cust.Orders in the Extension Method Syntax. Does anyone have any idea how it could work?

I got this but I have no access to the `CustomerName in the collection.

var allOrders2 =
    customers.SelectMany(cust => cust.Orders)
             .Join(products,
                   ord => ord.ProductID,
                   prod => prod.ProductID,
                   (ord, prod) => new
                   {
                       ord.ProductID,
                       OrderAmount = ord.Quantity * prod.Price
                   });
CSDev
  • 3,177
  • 6
  • 19
  • 37
Passhero
  • 23
  • 5
  • 1
    Any particular reason why you want to move to method syntax and not just stick with the query syntax? – Izzy Aug 01 '19 at 08:14
  • [Multiple field selection](https://stackoverflow.com/questions/1202981/select-multiple-fields-from-list-in-linq) might help you. – FeRaaC Aug 01 '19 at 08:20
  • It's absolutely overkill to create an extension method for this. Extension methods should be made very generic types and tasks. If this is motivated by reducing code replication, IMO you're hiding instead of fixing an architectural flaw. – Gert Arnold Aug 01 '19 at 08:38
  • Is there a general rule when to use query or extension methods? – Passhero Aug 01 '19 at 08:55

1 Answers1

3

If you order does not refer back to a customer, the trick is to first create a dataset which keeps the customers and orders linked together:

customers
   .SelectMany(c => c.Orders.Select(o => new {
       cust = c,
       ord = o
   }))

Then on this CustomerOrder (co) you can apply your join:

...
    .Join(products, 
        co => co.ord.ProductID, 
        prod => prod.ProductID, 
        (co,prod) => new {
          co.cust.Name,
          co.ord.ProductID, 
          OrderAmount = ord.Quantity * prod.Price});
CompuChip
  • 9,143
  • 4
  • 24
  • 48