【问题标题】:Using LINQ, how to convert a IList<IList<object>> to IList<object>?使用 LINQ,如何将 IList<IList<object>> 转换为 IList<object>?
【发布时间】:2012-04-15 06:02:45
【问题描述】:

我正在尝试将IList&lt;IList&lt;object&gt;&gt; 转换为IList&lt;object&gt;,我的意思是有一个唯一的列表,其中包含第一个的所有元素(对象)。

    public IList<IList<Page>> PMTs
    {
        get
        {
            var pmts = Processes.Select(x => x.PageMapTable)
                                .ToList();
            return pmts;
        }
    }

    public IList<Page> BlockMapTable
    {
        get
        {
            // Incomplete
            var btm = PMTs.Select(x => x.?? ..
        }
    }

【问题讨论】:

    标签: c# linq lambda linq-to-objects


    【解决方案1】:

    假设您想对list&lt;list&lt;Page&gt;&gt; 进行平面映射/展平,您可以使用 selectMany 方法来做到这一点,如下所示:

    public IList<Page> BlockMapTable
    {
        get
        {
            var btm = PMTs.SelectMany(x => x).ToList();
        }
    }
    

    如果你想了解更多,这里是great blog post about selectMany extension method

    【讨论】:

      【解决方案2】:
      public IList<Page> BlockMapTable
          {
              get
              {
                  return PMTs.SelectMany(p => p).ToList();
              }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-14
        • 2018-11-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-17
        相关资源
        最近更新 更多