C#比较两个 List 内容是否相同

开发过程中,遇到要比较两个List值是否相同的场景,比如考试答题跟答案是否相同来判断是否答对。

一、简单比较两个int类型的List是否相同,如下:

List<int> rightAnswerList = new List<int>() {1,2};//标准答案
List<int> examAnswerList = new List<int>() {1,3};//考试答题答案
bool isRight = examAnswerList.All(p => rightAnswerList.Any(r => r.Equals(p))) && examAnswerList.Count == rightAnswerList.Count;

二、开发过程中也许还好遇到比较两个其它类型的List,方法如下:

 /// <summary>
    /// 判断两个集合是否是相等的(所有的元素及数量都相等)
    /// </summary>
    /// <typeparam name="T">集合元素类型</typeparam>
    /// <param name="sourceCollection">源集合列表</param>
    /// <param name="targetCollection">目标集合列表</param>
    /// <returns>两个集合相等则返回True,否则返回False</returns>
    public static bool EqualList<T>(this IList<T> sourceCollection, IList<T> targetCollection) where T : IEquatable<T>
    {
        //空集合直接返回False,即使是两个都是空集合,也返回False
        if (sourceCollection == null || targetCollection == null)
        {
            return false;
        }
 
        if (object.ReferenceEquals(sourceCollection, targetCollection))
        {
            return true;
        }
 
        if (sourceCollection.Count != targetCollection.Count)
        {
            return false;
        }
 
        var sourceCollectionStaticsDict = sourceCollection.StatisticRepetition();
        var targetCollectionStaticsDict = targetCollection.StatisticRepetition();
 
        return sourceCollectionStaticsDict.EqualDictionary(targetCollectionStaticsDict);
    }
 
    /// <summary>
    /// 判断两个字典是否是相等的(所有的字典项对应的值都相等)
    /// </summary>
    /// <typeparam name="TKey">字典项类型</typeparam>
    /// <typeparam name="TValue">字典值类型</typeparam>
    /// <param name="sourceDictionary">源字典</param>
    /// <param name="targetDictionary">目标字典</param>
    /// <returns>两个字典相等则返回True,否则返回False</returns>
    public static bool EqualDictionary<TKey, TValue>(this Dictionary<TKey, TValue> sourceDictionary, Dictionary<TKey, TValue> targetDictionary)
        where TKey : IEquatable<TKey>
        where TValue : IEquatable<TValue>
    {
        //空字典直接返回False,即使是两个都是空字典,也返回False
        if (sourceDictionary == null || targetDictionary == null)
        {
            return false;
        }
 
        if (object.ReferenceEquals(sourceDictionary, targetDictionary))
        {
            return true;
        }
 
        if (sourceDictionary.Count != targetDictionary.Count)
        {
            return false;
        }
 
        //比较两个字典的Key与Value
        foreach (KeyValuePair<TKey, TValue> item in sourceDictionary)
        {
            //如果目标字典不包含源字典任意一项,则不相等
            if (!targetDictionary.ContainsKey(item.Key))
            {
                return false;
            }
 
            //如果同一个字典项的值不相等,则不相等
            if (!targetDictionary[item.Key].Equals(item.Value))
            {
                return false;
            }
        }
 
        return true;
    }
 
    /// <summary>
    /// 统计集合的重复项,并返回一个字典
    /// </summary>
    /// <typeparam name="T">集合元素类型</typeparam>
    /// <param name="sourceCollection">统计集合列表</param>
    /// <returns>返回一个集合元素及重复数量的字典</returns>
    private static Dictionary<T, int> StatisticRepetition<T>(this IEnumerable<T> sourceCollection) where T : IEquatable<T>
    {
        var collectionStaticsDict = new Dictionary<T, int>();
        foreach (var item in sourceCollection)
        {
            if (collectionStaticsDict.ContainsKey(item))
            {
                collectionStaticsDict[item]++;
            }
            else
            {
                collectionStaticsDict.Add(item, 1);
            }
        }
 
        return collectionStaticsDict;
    }

后面这个方法是另一个博主写的,参考一下,原文链接:https://blog.csdn.net/u014661152/article/details/107098263

posted @ 2022-11-17 17:00  清和时光  阅读(4891)  评论(1编辑  收藏  举报