sql 是通过引用到 Linq 联合吗? (除了和相交一样吗?)

Is sql to Linq union by reference? (the same for except and intersect?)

当你在 linq to sql 中执行联合时,它是通过引用 - 如果对对象的引用指向同一个对象 - 还是通过 id 字段?

示例方法:

        public IEnumerable<CustomerModel> GetCustomers(string searchCriteria)
    {
        ExpressLogger.LogDebug("Enter"); 
        try
        {
            var datacontext = Helpers.Utils.GetCustomerRmhDataContext();
            using(datacontext)
            {
               var accountCustomers = datacontext.Customers.Where(c => 
                            c.AccountNumber.ToLower().Contains(searchCriteria.ToLower())).Select(c=>convertRmhToModel(c,datacontext)).ToList();

                var phoneCustomers = datacontext.Customers.Where(c =>
                            c.PhoneNumber.ToLower().Contains(searchCriteria.ToLower())).Select(c => convertRmhToModel(c, datacontext)).ToList();

                var personalNumberCustomers = datacontext.Customers.Where(c =>
                            c.TaxNumber.ToLower().Contains(searchCriteria.ToLower())).Select(c => convertRmhToModel(c, datacontext)).ToList();

                var emailCustomers = datacontext.Customers.Where(c =>
                            c.EmailAddress.ToLower().Contains(searchCriteria.ToLower())).Select(c => convertRmhToModel(c, datacontext)).ToList();


                var allCustomers = accountCustomers.Union(phoneCustomers).Union(personalNumberCustomers).Union(emailCustomers).ToList();


                return allCustomers;
            }
        }
        catch (Exception ex)
        {
            ExpressLogger.LogError(ex.Message);
            throw;
        }
    }

这个功能做我想做的,它搜索客户的帐号、phone、邮件和瑞典个人号码。然后我创建一个联合并 return 结果。但我很好奇 - 这是如何工作的,它将联合与 ID 字段进行比较还是通过引用进行比较 - 它会检查哈希码和相等比较器吗? (intersect 和 except 是一样的吗?)

此时,在 .Net 集合上调用 .Union,其行为在 Enumerable.Union 的文档中定义:

The default equality comparer, Default, is used to compare values of the types that implement the IEqualityComparer generic interface.

EqualityComparer<T>.Default 的文档中进一步阐明了这一点:

The Default property checks whether type T implements the System.IEquatable<T> interface and, if so, returns an EqualityComparer<T> that uses that implementation. Otherwise, it returns an EqualityComparer<T> that uses the overrides of Object.Equals and Object.GetHashCode provided by T.

因此,如果 CustomerModel 实施 IEquatable<CustomerModel>.Equals and/or 覆盖 Object.Equals,则 Union 使用其中一种方法来比较客户。

否则,它会返回到 Object.Equals 的默认实现来比较对象引用:

If the current instance is a reference type, the Equals(Object) method tests for reference equality, and a call to the Equals(Object) method is equivalent to a call to the ReferenceEquals method..