简体   繁体   中英

C# EntityFramework Join

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.

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

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