C# 【List拓展】两个List比较是否相同

using System;

public static class ListExpand
{
    /// <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;
    }
}

使用时直接使用字段调取EqualList

//查询数据返回list
List<string> tempQueryList = DBSvc.instance.QueryData();

        
//查询不到数据|查询数据与现有数据相等
if (tempQueryList==null | dataList.EqualList(tempQueryList))
{
     
    return;
  
}

如果运行成功,求个赞赞赞赞赞

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要获取两个 List 中的相同部分,你可以使用 LINQ 的 `Intersect` 方法。下面是一个示例: ```csharp List<int> list1 = new List<int> { 1, 2, 3, 4 }; List<int> list2 = new List<int> { 3, 4, 5, 6 }; List<int> commonItems = list1.Intersect(list2).ToList(); ``` 在上述代码中,`commonItems` 列表将包含 `list1` 和 `list2` 中相同的元素(3 和 4)。 如果你想要获取相同部分的其他类型的对象,你可以使用 `Intersect` 方法的重载版本,并提供自定义的 `IEqualityComparer` 来指定比较逻辑。例如,如果你有一个自定义类 `Person`,你可以按照特定的属性进行比较: ```csharp public class Person { public string Name { get; set; } public int Age { get; set; } } List<Person> list1 = new List<Person> { new Person { Name = "Alice", Age = 25 }, new Person { Name = "Bob", Age = 30 }, }; List<Person> list2 = new List<Person> { new Person { Name = "Bob", Age = 30 }, new Person { Name = "Charlie", Age = 35 }, }; List<Person> commonPeople = list1.Intersect(list2, new PersonComparer()).ToList(); public class PersonComparer : IEqualityComparer<Person> { public bool Equals(Person x, Person y) { return x.Name == y.Name && x.Age == y.Age; } public int GetHashCode(Person obj) { return obj.Name.GetHashCode() ^ obj.Age.GetHashCode(); } } ``` 在上述代码中,`commonPeople` 列表将包含 `list1` 和 `list2` 中具有相同姓名和年龄的 Person 对象。 使用 `Intersect` 方法可以轻松地获取两个 List相同部分。根据你的需求,你可能需要进一步处理结果或使用其他方法来实现更复杂的逻辑。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

春天花花幼稚园的程序员教孩子如何玩键盘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值