判断两个list是否含有重复值数据是否相同

时间:2020-09-02 17:07:16   收藏:0   阅读:321
  1. 比较代码 Except方法需要using System.Linq;
private bool CompareList(List<string>? beforeContractConsumerInbounds, List<string>? afterContractConsumerInbounds)
{
    if (beforeContractConsumerInbounds == null || beforeContractConsumerInboundCount == 0 
    || afterContractConsumerInbounds == null || afterContractConsumerInboundCount == 0)
        return false
    List<string> beforeConsumerInbounds = GetHashSortResult(beforeContractConsumerInbounds);
    List<string> afterConsumerInbounds = GetHashSortResult(afterContractConsumerInbounds)
    return beforeConsumerInbounds.Except(afterConsumerInbounds).Any() == false &&
           afterConsumerInbounds.Except(beforeConsumerInbounds).Any() == false;
}
  1. 得到过滤相同值后的结果
protected List<string> GetHashSortResult(List<string> listModel)
{
    HashSet<string> hashString = new HashSet<string>();
    foreach (string item in listModel)
    {
        hashString.Add(item);
    }
    List<string> listResult = new List<string>();
    listResult.AddRange(hashString.ToArray());
    return listResult;
}

原文:https://www.cnblogs.com/andykk/p/13602383.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!