IQueryable.Distinct()VS名单< T> .Distinct() [英] IQueryable.Distinct() vs List<T>.Distinct()

查看:1113
本文介绍了IQueryable.Distinct()VS名单< T> .Distinct()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我使用鲜明的()LINQ查询。如果我只是调用鲜明的(),无须转换为一个列表,则它不会返回一个独特的名单 - 它仍包含重复

I have a linq query that I am using Distinct() on. If I just call Distinct() without converting to a List then it does not return a distinct list - it still contains duplicates.

但是,如果我转换到一个列表和然后的调用鲜明的() - 它按预期工作,我只得到唯一对象

However if I convert to a List and then call Distinct() - it works as expected and I only get unique objects.

我使用Telerik的ORM和返回的对象。表示数据库中的一个表的类。

I'm using Telerik ORM and the objects being returned are the class representing one of the tables in the database.

var uniqueUsers = (from u in Database.Users 
                   select u).Distinct();



上面的代码不会产生明显的效果,但是当我转换到一个列表,并调用不同 - 它不:

The code above does not produce distinct results, however when I convert to a list and call distinct - it does:

var uniqueUsers = (from u in Database.Users 
                   select u).ToList().Distinct();



我怀疑这被转换成一个列表之前的集合做的,比较的引用的对象,而不是对象数据本身,但我不完全明白是怎么回事 - 为什么拳头代码示例不能产生独特的效果,并使用.ToList(),使得它工作的时候会发生什么情况的收集?

I suspect this has to do with the collection before being converted to a list, comparing references to objects rather than the object data itself but I do not fully understand what is going on - why does the fist code example not produce unique results and what happens to the collection when using .ToList() that makes it work?

*编辑* 我却简化了上述疑问,在现实世界中查询有几个联接产生非唯一的结果,我的上午的只返回用户对象。

* EDIT * I've simplified the above queries, in the real world the query has several joins which generates non-unique results, however I am returning just the User objects.

我试图重写等于的GetHashCode 方法,但是这并没有任何区别。

I tried overriding the Equals and GetHashCode methods but this did not make any difference.

public override bool Equals(object obj)
{
    User comparingObject = obj as User ;

    if (comparingObject == null)
    {
        return false;
    }
    else
    {
        return comparingObject.UserID.Equals(this.UserID);
    }
}

public override int GetHashCode()
{
    return this.UserID.GetHashCode();
}



感谢。

Thanks.

[更新]
有跑LinqPad同一查询,它将按预期工作提供了不同的条目列表。但是使用Telerik的ORM的DLL,我得到的多个条目时,运行在LinqPad相同的查询。因此,它的出现的是与Telerik的一个特点。当我有时间,我会进一步调查,并与Telerik的支持,提高它。

[UPDATE] Having ran the same query in LinqPad, it works as expected providing a list of distinct entries. However running the same query in LinqPad when using the Telerik ORM dll I get multiple entries. So it appears to be a peculiarity with Telerik. When I have time I will investigate further and raise it with Telerik support.

推荐答案

显然,你不能有确切的重复行(包括主键)在表格中。也许你的意思是用一些相同的字段(不包括主键)行。

Obviously you cannot have exact duplicate rows (including the primary key) in your table. Probably what you mean is rows with some equal fields (excluding primary key).

调用鲜明的IQueryable ,产生的查询结果,你的表的每一个领域比较针对对方一个SQL DISTINCT 运营商。因为你不能在表中准确重复行,它返回的所有行。

Calling Distinct on IQueryable, generates a SQL DISTINCT operator on the resulting query, which compares every field of your table against each other. Because you cannot have exact duplicate rows in the table, it returns all the rows.

在另一方面,要求鲜明列表<使用者> 将使用等于用户的方法对象在内存比较对象(从数据库中获取所有行之后)。最终的结果取决于等于的方法,它可以检查只对相等的值某些字段。

On the other hand, calling Distinct on a List<User> will use Equals method of the User object to compare objects in memory (after fetching all the rows from database). The final result depends on the implementation of Equals method, which could check only some fields for equal values.

这篇关于IQueryable.Distinct()VS名单&LT; T&GT; .Distinct()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆