【问题标题】:Entity Framework query to get child items [duplicate]实体框架查询以获取子项[重复]
【发布时间】:2019-07-21 06:17:07
【问题描述】:

我的订单结构如下

订单 1234

  • 第 1 类

    • 项目 111

    • 第 222 项

  • 第 7 类

    • 项目 444

如何修改下面的查询以包含订单 1234 中的所有商品

public List<Items> GetAllOrderItems(int orderId)
{

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

List&lt;Items&gt; => 项目 111、项目 222、项目 444

【问题讨论】:

  • 也许 Order 的类定义会有所帮助
  • 你试过用谷歌搜索这个吗?已经有几十个答案了。
  • 我认为您需要选择s.Category,然后使用SelectMany 获取项目。

标签: c# entity-framework entity-framework-6 linq-to-entities


【解决方案1】:

您可以为此使用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);
    }
}

【讨论】:

    猜你喜欢
    • 2011-12-06
    • 2018-11-16
    • 1970-01-01
    • 2011-12-18
    • 1970-01-01
    • 2018-03-18
    • 2023-03-20
    相关资源
    最近更新 更多