zoukankan      html  css  js  c++  java
  • Enumerable.Union<TSource> 方法

    功能:生成两个序列的并集(使用默认的相等比较器)。

    命名空间: System.Linq

    程序集: System.Core.dll

    备注:实现此方法时使用了延迟执行。 它直接返回一个对象,该对象存储了执行操作所需的所有信息。 此方法所表示的查询不会被执行,直到调用了 GetEnumerator 方法或通过使用了 Visual C# 中的 foreach 或 Visual Basic 中的 For Each。
    此方法从结果集中排除重复项。 这和 Concat<TSource> 方法(它的功能是连接两个序列)不同,后者返回输入序列中的所有元素包括重复项。
    默认的相等比较器,Default ,用于比较的类型的值,它实现了 IEqualityComparer<T> 泛型接口。 若自定义数据类型比较器,您需要实现此接口,并提供您自己的 GetHashCode 和 Equals 方法。
    当此方法返回的对象是枚举对象时, Union<TSource> 枚举 first 和 second 中的对象并按此顺序来生成尚未生成的每个元素。
    示例

    下面的代码示例演示如何使用 Union<TSource>(IEnumerable<TSource>, IEnumerable<TSource>) 以获取两个整数的序列的并集。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    public class Test
    {
        static void Main()
        {
            int[] ints1 = { 5, 3, 9, 7, 5, 9, 3, 7 };
            int[] ints2 = { 8, 3, 6, 4, 4, 9, 1, 0 };
    
            IEnumerable<int> union = ints1.Union(ints2);
    
            foreach (int num in union)
            {
                Console.Write("{0} ", num);
            }
    
            /*
             程序运行结果:
             5 3 9 7 8 6 4 1 0
            */
        }
    }

    如果您想要自定义数据比较器,则必须实现 IEqualityComparer<T> 泛型接口。下面的代码演示如何实现此接口,并提供对 GetHashCode 和 Equals 方法的实现。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    public class ProductA
    {
        public string Name { get; set; }
        public int Code { get; set; }
    
        public virtual bool MyEquals(Object obj)
        {
            return true;
        }
    }
    
    public class ProductComparer : IEqualityComparer<ProductA>
    {
        public bool Equals(ProductA x, ProductA y)
        {
            //检查比较的对象的引用是否一样
            if (Object.ReferenceEquals(x, y))
                return true;
    
            //检查product对象的属性的值是否相等
            return x != null && y != null && x.Code.Equals(y.Code) && x.Name.Equals(y.Name);
        }
    
        public int GetHashCode(ProductA obj)
        {
            //如果Name字段不为空,就获取它的哈希编码
            int hashProductName = obj.Name == null ? 0 : obj.Name.GetHashCode();
    
            //获取Code字段的哈希编码
            int hashProductCode = obj.Code.GetHashCode();
    
            //计算product对象的哈希编码
            return hashProductName ^ hashProductCode;
        }
    }
    
    public class Test
    {
        static void Main()
        {
            ProductA[] store1 =
            {
                    new ProductA { Name = "apple", Code = 9 },
                    new ProductA { Name = "orange", Code = 4 }
                };
            ProductA[] store2 =
            {
                    new ProductA { Name = "apple", Code = 9 },
                    new ProductA { Name = "lemon", Code = 12 }
                };
    
            // 从上面两个数组中获取products,并排除相同的项,
    // 这里使用了自定义的比较器:ProductComparer
    IEnumerable<ProductA> union = store1.Union(store2, new ProductComparer()); foreach (var product in union) Console.WriteLine(product.Name + " " + product.Code); /* 程序运行结果: apple 9 orange 4 lemon 12 */ } }

    参考资料:https://msdn.microsoft.com/zh-cn/library/bb341731(v=vs.110).aspx

  • 相关阅读:
    React class & function component 的区别
    Webpack 4 + React + Typescript 搭建启动模版
    JavaScript的数据类型及其检测
    react SyntheticEvent 合成事件机制
    数组的排序
    Ajax 和 Ajax的封装
    go 结构的方法总结
    go 结构的方法2
    go struct 的方法1
    go 函数闭包
  • 原文地址:https://www.cnblogs.com/hellowzl/p/6398029.html
Copyright © 2011-2022 走看看