与linq的连接查询不同

时间:2015-10-10 14:23:48

标签: c# asp.net-mvc linq

我得到3行同样的评论作者,我如何做到与众不同1评论作者

--> Call the `Hantera` method in the `Withdraw` class  
--> Change the List in the "Statement"/Log class  
--> Call the `Hantera` method in "Statement"/Log class  
--> Doesn't print out anything

1 个答案:

答案 0 :(得分:1)

您可以使用Distinct。您可能必须实现自己的自定义比较器。

var uniqueCommentingAuthors = CommentingAuthor.Distinct();

使用客户比较器:

public class CommentingAuthorComparer : IEqualityComparer<CommentingAuthor>
{
    public bool Equals(CommentingAuthor author, CommentingAuthor author2)
    {
        return author.PostAuthorName.Equals(author2.PostAuthorName);
    }

    public int GetHashCode(CommentingAuthor author)
    {
        return author.PostAuthorName.GetHashCode();
    }
}

然后你就可以使用它:

   var comparer = new CommentingAuthorComparer();
   var uniqueAuthors = CommentingAuthor.Distinct(comparer);
   return View(uniqueAuthors);