0

I would like to join the results of 2 entities based on the quantities. Here's what I have...

Order Ticket #1

OrderID    CustomerID    ItemID    Description    POS#    Cost    Quantity
1          1             1         Apples         111     1.25    3
1          1             2         Oranges        222     1.12    5
1          1             3         Bananas        333     1.17    5

Order Ticket #2

OrderID    CustomerID    ItemID    Description    POS#    Cost    Quantity
2          1             1         Apples         111     1.25    7
2          1             2         Oranges        222     1.12    2
2          1             3         Bananas        333     1.17    5

Here is the code I use to get each ticket:

public OrderEntity getOrder(int orderId)
{
    var data = from c in Orders
               where c.OrderID == orderId
               select c;
    return data;
}

How would I write the LINQ code to combine the 2 tickets so I get a Sum of the quantities? It should look like this...

Order Tickets #1 and #2

CustomerID    ItemID    Description    POS#    Cost    Quantity
1             1         Apples         111     1.25    10
1             2         Oranges        222     1.12    7
1             3         Bananas        333     1.17    10

It seems like I should be able to do something like so...

public List<OrderEntity> getCustomerOrders(int customerId)
{
    var data = from c in Orders
               where c.CustomerID == customerId
               select c;
    return data.ToList();
}

The problem is I cannot figure out the grouping. There is a lot of info about there about how to write the EF code for grouping, but I'm not sure if I should be grouping on CustomerID or on the Quantity. Any tips on how to do the grouping here would be greatly appreciated.

2
  • where is your OrderID column? Commented Feb 23, 2015 at 3:55
  • how is OrderEntity defined? Commented Feb 23, 2015 at 4:04

1 Answer 1

2

You should group by CustomerID and ItemID:

Try something like this:

public List<OrderEntity> getCustomerOrders(int customerId)
{
    var data = from c in Orders
               where c.CustomerID == customerId
               group c by new { c.CustomerID, c.ItemID } into g
               select new OrderEntity () {
                  CustomerID = g.Key.CustomerID,
                  ItemID =  g.Key.ItemID,
                  Quantity = g.Sum(x => x.Quantity) 
               };
    return data.ToList();
}

I'm not sure how you define your data, but if you need to have Description POS, Cost in your result, try:

public List<OrderEntity> getCustomerOrders(int customerId)
    {
        var data = from c in Orders
                   where c.CustomerID == customerId
                   group c by new { c.CustomerID, c.ItemID,c.Description,c.POST,c.Cost } into g
                   select new OrderEntity () {
                      CustomerID = g.Key.CustomerID,
                      ItemID =  g.Key.ItemID,
                      Quantity = g.Sum(x => x.Quantity),
                      Description = g.Key.Description,
                      POST = g.Key.POST,
                      Cost = g.Key.Cost 
                   };
        return data.ToList();
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.