Distinct 不能使用 LINQ to Objects [英] Distinct not working with LINQ to Objects

查看:28
本文介绍了Distinct 不能使用 LINQ to Objects的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class Program
{
    static void Main(string[] args)
    {
        List<Book> books = new List<Book> 
        {
            new Book
            {
                Name="C# in Depth",
                Authors = new List<Author>
                {
                    new Author 
                    {
                        FirstName = "Jon", LastName="Skeet"
                    },
                     new Author 
                    {
                        FirstName = "Jon", LastName="Skeet"
                    },                       
                }
            },
            new Book
            {
                Name="LINQ in Action",
                Authors = new List<Author>
                {
                    new Author 
                    {
                        FirstName = "Fabrice", LastName="Marguerie"
                    },
                     new Author 
                    {
                        FirstName = "Steve", LastName="Eichert"
                    },
                     new Author 
                    {
                        FirstName = "Jim", LastName="Wooley"
                    },
                }
            },
        };


        var temp = books.SelectMany(book => book.Authors).Distinct();
        foreach (var author in temp)
        {
            Console.WriteLine(author.FirstName + " " + author.LastName);
        }

        Console.Read();
    }

}
public class Book
{
    public string Name { get; set; }
    public List<Author> Authors { get; set; }
}
public class Author
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public override bool Equals(object obj)
    {
        return true;
        //if (obj.GetType() != typeof(Author)) return false;
        //else return ((Author)obj).FirstName == this.FirstName && ((Author)obj).FirstName == this.LastName;
    }

}

这是基于LINQ in Action"中的一个例子.清单 4.16.

This is based on an example in "LINQ in Action". Listing 4.16.

这会打印 Jon Skeet 两次.为什么?我什至尝试在 Author 类中覆盖 Equals 方法.仍然 Distinct 似乎不起作用.我错过了什么?

This prints Jon Skeet twice. Why? I have even tried overriding Equals method in Author class. Still Distinct does not seem to work. What am I missing?

我也添加了 == 和 != 运算符重载.仍然没有帮助.

I have added == and != operator overload too. Still no help.

 public static bool operator ==(Author a, Author b)
    {
        return true;
    }
    public static bool operator !=(Author a, Author b)
    {
        return false;
    }

推荐答案

LINQ Distinct 在自定义对象方面并不是那么聪明.

LINQ Distinct is not that smart when it comes to custom objects.

它所做的只是查看您的列表,看看它有两个不同的对象(它不关心它们的成员字段值是否相同).

All it does is look at your list and see that it has two different objects (it doesn't care that they have the same values for the member fields).

一种解决方法是实现 IEquatable 接口,如此处所示.

One workaround is to implement the IEquatable interface as shown here.

如果你像这样修改你的 Author 类,它应该可以工作.

If you modify your Author class like so it should work.

public class Author : IEquatable<Author>
{
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public bool Equals(Author other)
    {
        if (FirstName == other.FirstName && LastName == other.LastName)
            return true;

        return false;
    }

    public override int GetHashCode()
    {
        int hashFirstName = FirstName == null ? 0 : FirstName.GetHashCode();
        int hashLastName = LastName == null ? 0 : LastName.GetHashCode();

        return hashFirstName ^ hashLastName;
    }
}

试试 DotNetFiddle

这篇关于Distinct 不能使用 LINQ to Objects的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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