【问题标题】:How to concatenate ReadOnlyCollection<string> and IList<string>如何连接 ReadOnlyCollection<string> 和 IList<string>
【发布时间】:2016-06-21 10:24:57
【问题描述】:

我有 readonlucollection 和 IList 的两个属性 并创建了List&lt;string&gt; 的另一个属性 OptionListreadonlycollection&lt;string&gt;ForListIList&lt;string&gt; 但我没有在组合列表中获得完整列表,它跳过了optionlist 中的一些元素。

我如何获得完整列表?

public List<string> CombineList {
    get {
        return OptionList.Union(ForList).ToList();
    }
}

【问题讨论】:

    标签: c# list concat


    【解决方案1】:

    Union 方法跳过两个列表共有的元素。如果您想要一个保留重复项的列表,请使用串联而不是联合:

    return OptionList.Concat(ForList).ToList();
    

    【讨论】:

      【解决方案2】:

      如果要连接,请使用ConcatUnion 有一个隐式的 distinct 内置。

      【讨论】:

        【解决方案3】:

        问题可能是Union,而您没有使用Concat。下面的帖子中描述了略有不同:(希望这会有所帮助:))

        在这篇文章中:Union Vs Concat in Linq

        我会推荐这种方法:

        OptionList.Concat(forList).ToList();
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-02-27
          • 2011-12-29
          • 2010-09-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多