2

I have order structure as below

Order 1234

  • Category 1

    • Item 111

    • Item 222

  • Category 7

    • Item 444

How can I modify the query below to include all items from order 1234

public List<Items> GetAllOrderItems(int orderId)
{

   var result = (from o in _orderContext.Orders
                 where o.OrderId == orderId
                 select s).toList();
}

List<Items> => Item 111,Item 222, Item 444

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
mehwish
  • 199
  • 3
  • 15

1 Answers1

1

you can use SelectMany for this. Here a simple example:

class Master
{
    public List<Item> Items { get; set; } = new List<Item>() { new Item(1), new Item(2) };
}
class Item
{
    public Item(int x)
    {
        this.X = x;
    }

    public int X { get; set; }
}

public static void Main(string[] args)
{
    var masters = new List<Master>();
    masters.Add(new Master());
    masters.Add(new Master());

    List<int> xList = masters.Select(s => s.Items).SelectMany(s => s, (m, i) => i.X).ToList();

    foreach (int item in xList)
    {
        Console.WriteLine(item);
    }
}
kara
  • 3,205
  • 4
  • 20
  • 34