不使用LINQto对象的区别

不使用LINQto对象的区别

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。

这印了乔恩·斯基特两次。为什么?我甚至在Author类中尝试过重写等于方法。仍然不同似乎不起作用。我遗漏了什么?



郎朗坤
浏览 375回答 3
3回答

蝴蝶刀刀

这个Distinct()方法检查引用类型的引用相等性。这意味着它在寻找相同的复制对象,而不是包含相同值的不同对象。有一个过载这需要一个质量检验员,因此您可以指定不同的逻辑来确定给定对象是否等于另一个对象。如果希望Author的行为通常类似于普通对象(即仅引用相等),但为了根据名称值进行不同的检查相等,请使用质量检验员..如果您总是希望根据名称值对Author对象进行比较,那么重写GetHashCode并等于,或实施I赤道..上的两个成员IEqualityComparer接口Equals和GetHashCode..你的逻辑决定两个Author如果名字字符串和姓字符串相同,则对象看起来是相等的。public&nbsp;class&nbsp;AuthorEquals&nbsp;:&nbsp;IEqualityComparer<Author>{ &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;bool&nbsp;Equals(Author&nbsp;left,&nbsp;Author&nbsp;right) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if((object)left&nbsp;==&nbsp;null&nbsp;&&&nbsp;(object)right&nbsp;==&nbsp;null) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;true; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if((object)left&nbsp;==&nbsp;null&nbsp;||&nbsp;(object)right&nbsp;==&nbsp;null) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;false; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;left.FirstName&nbsp;==&nbsp;right.FirstName&nbsp;&&&nbsp;left.LastName&nbsp;==&nbsp;right.LastName; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;int&nbsp;GetHashCode(Author&nbsp;author) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;(author.FirstName&nbsp;+&nbsp;author.LastName).GetHashCode(); &nbsp;&nbsp;&nbsp;&nbsp;}}

吃鸡游戏

另一个没有实现的解决方案IEquatable,&nbsp;Equals和GetHashCode是使用LINQGroupBy方法,并从IGrouping中选择第一个项。var&nbsp;temp&nbsp;=&nbsp;books.SelectMany(book&nbsp;=>&nbsp;book.Authors) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.GroupBy&nbsp;(y&nbsp;=>&nbsp;y.FirstName&nbsp;+&nbsp;y.LastName&nbsp;) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Select&nbsp;(y&nbsp;=>&nbsp;y.First&nbsp;());foreach&nbsp;(var&nbsp;author&nbsp;in&nbsp;temp){ &nbsp;&nbsp;Console.WriteLine(author.FirstName&nbsp;+&nbsp;"&nbsp;"&nbsp;+&nbsp;author.LastName);}
打开App,查看更多内容
随时随地看视频慕课网APP