Distinct not working with LINQ to Objects

后端 未结 9 1826
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 12:20
class Program
{
    static void Main(string[] args)
    {
        List books = new List 
        {
            new Book
            {
                


        
9条回答
  •  眼角桃花
    2020-11-22 12:59

    Another solution without implementing IEquatable, Equals and GetHashCode is to use the LINQs GroupBy method and to select the first item from the IGrouping.

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

提交回复
热议问题