我的订单结构如下
订单 1234
我如何修改下面的查询以包含订单 1234 中的所有项目
public List<Items> GetAllOrderItems(int orderId)
{
var result = (from o in _orderContext.Orders
where o.OrderId == orderId
select s).toList();
}
List<Items>
=> 项目 111,项目 222,项目 444
您可以为此使用SelectMany
。这里有一个简单的例子:
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);
}
}