На основе @ ответ Гранди, в том числе реализации:
public class MovieEqualityComparer : IEqualityComparer<Movie>
{
public bool Equals(Movie x, Movie y)
{
if (x == null)
return y == null;
if (y == null)
return x == null;
if (object.ReferenceEquals(x, y))
return true;
if (!string.Equals(x.MovieName, y.MovieName))
return false;
if (!string.Equals(x.MovieRating, y.MovieRating))
return false;
if (!string.Equals(x.MovieRuntime, y.MovieRuntime))
return false;
if (!Enumerable.SequenceEqual(x.MovieActors, y.MovieActors))
return false;
if (!Enumerable.SequenceEqual(x.MovieImages, y.MovieImages))
return false;
return true;
}
public int GetHashCode(Movie obj)
{
if (obj == null)
throw new ArgumentNullException();
unchecked
{
int hash = 17;
hash = hash * 31 + ((obj.MovieName == null) ? 0 : obj.MovieName.GetHashCode());
hash = hash * 31 + ((obj.MovieRating == null) ? 0 : obj.MovieRating.GetHashCode());
hash = hash * 31 + ((obj.MovieRuntime == null) ? 0 : obj.MovieRuntime.GetHashCode());
if (obj.MovieActors != null)
{
foreach (var actor in obj.MovieActors)
hash = hash * 31 + ((actor == null) ? 0 : actor.GetHashCode());
}
if (obj.MovieImages != null)
{
foreach (var image in obj.MovieImages)
hash = hash * 31 + ((image == null) ? 0 : image.GetHashCode());
}
return hash;
}
}
}
Использование является тем же самым:
List<Movie> myMovies = new List<Movie>
{
// ...
};
List<Movie> myDistinctMovies = myMovies.Distinct(new MovieEqualityComparer()).ToList();
EDIT
Как отметил @ Тим, вы должны сделать что-то очень похожее на других ваших пользовательских типов, если вы хотите сравнить ничем, кроме равенство ссылок.
public class Actor : IEquatable<Actor>
{
public string ActorName;
public string ActorRole;
public override bool Equals(object obj)
{
return this.Equals(obj as Actor);
}
public bool Equals(Actor other)
{
if (other == null)
return false;
if (object.ReferenceEquals(this, other))
return true;
if (!string.Equals(this.ActorName, other.ActorName))
return false;
if (!string.Equals(this.ActorRole, other.ActorRole))
return false;
return true;
}
public override int GetHashCode()
{
unchecked
{
int hash = 17;
hash = hash * 31 + ((ActorName == null) ? 0 : ActorName.GetHashCode());
hash = hash * 31 + ((ActorRole == null) ? 0 : ActorRole.GetHashCode());
return hash;
}
}
}
public class MovieArt : IEquatable<MovieArt>
{
public string ImagePath;
public override bool Equals(object obj)
{
return this.Equals(obj as MovieArt);
}
public bool Equals(MovieArt other)
{
if (other == null)
return false;
if (object.ReferenceEquals(this, other))
return true;
if (!string.Equals(this.ImagePath, other.ImagePath))
return false;
return true;
}
public override int GetHashCode()
{
unchecked
{
int hash = 17;
hash = hash * 31 + ((ImagePath == null) ? 0 : ImagePath.GetHashCode());
return hash;
}
}
}
вы можете использовать [Distinct с EqualityComparer] (http://msdn.microsoft.com/ ru-ru/library/bb338049 (v = vs.110) .aspx) – Grundy
Вы можете переопределить Equals и GetHashCode типа данных Movie. – Peter
@ Grundy/peer .. Пример будет отличным .. –