如何连接 ReadOnlyCollection<string> 和 IList<string>

How to concatenate ReadOnlyCollection<string> and IList<string>

我有两个 属性 用于 readonlucollection 和 IList 并创建了另一个 属性 of List<string> OptionListreadonlycollection<string>ForListIList<string> 但我没有在组合列表中获得完整列表,它跳过了 optionlist.

中的一些元素

如何获取完整列表?

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

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

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

如果要串联,请使用 ConcatUnion 有一个隐含的 distinct 内置。

问题可能是 Union并且您没有使用 Concat。下面的 post 中描述了一个细微的差别:(希望对您有所帮助 :) )

在此 post 中:Union Vs Concat in Linq

我会推荐这种方法:

OptionList.Concat(forList).ToList();