gpt4 book ai didi

c# - 选择不同于 List 忽略一个元素

转载 作者:太空狗 更新时间:2023-10-29 17:56:14 24 4
gpt4 key购买 nike

我有一个自定义数据类型列表,简化示例 (myMovies):

public class Movie
{
public Int32 TVIndex;
public string MovieName;
public string MovieRating;
public string MovieRuntime;
public List<Actor> MovieActors;
public List<MovieArt> MovieImages;
}



public class Actor
{
public string ActorName;
public string ActorRole;
}

public class MovieArt
{
public string ImagePath;
}


List<Movie> myMovies = new List<Movie>();

现在我试图从 myMovies 中删除所有重复项,但忽略 TVIndex

我试过看

List<Movie> myDistinctMovies = myMovies.Distinct().ToList(); 

但不知道如何忽略 TvIndex。这可能吗?

最佳答案

基于@Grundy 的回答,包括实现:

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();

编辑

正如@Tim 所指出的,如果您想通过引用相等性以外的任何方式进行比较,则必须对您的其他自定义类型做一些非常相似的事情。

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;
}
}
}

关于c# - 选择不同于 List<t> 忽略一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21676859/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com