2016-06-21 54 views
1

我有两个属性readonlucollection和IList的 创造的List<string> OptionListreadonlycollection<string>另一个属性和ForListIList<string> 但我不明白的结合列表完整列表它跳过一些元素从optionlist如何连接ReadOnlyCollection <string>和IList的<string>

我如何获得完整列表?

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

回答

2

Union方法跳过了这两个列表共有的元素。如果你想有,保持重复列表,使用连接而不是联盟:

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

使用Concat,如果你想连接。 Union有一个隐式的独特的内置。

0

问题可能是Union而且您不使用Concat。有这将在下面的描述后略有性差异:(希望这helpes :))

在这篇文章:Union Vs Concat in Linq

我会建议这种做法:

OptionList.Concat(forList).ToList();