鉴于以下课程
public class Foo
{
public int FooId { get; set; }
public string FooName { get; set; }
public override bool Equals(object obj)
{
Foo fooItem = obj as Foo;
if (fooItem == null)
{
return false;
}
return fooItem.FooId == this.FooId;
}
public override int GetHashCode()
{
// Which is preferred?
return base.GetHashCode();
//return this.FooId.GetHashCode();
}
}
Run Code Online (Sandbox Code Playgroud)
我已经覆盖了该Equals方法,因为它Foo代表了Foos表的一行.哪个是覆盖的首选方法GetHashCode?
覆盖为什么重要GetHashCode?
这就是我想要做的.我正在使用LINQ to XML查询XML文件,它为我提供了一个IEnumerable <T>对象,其中T是我的"Village"类,填充了此查询的结果.有些结果是重复的,所以我想在IEnumerable对象上执行Distinct(),如下所示:
public IEnumerable<Village> GetAllAlliances()
{
try
{
IEnumerable<Village> alliances =
from alliance in xmlDoc.Elements("Village")
where alliance.Element("AllianceName").Value != String.Empty
orderby alliance.Element("AllianceName").Value
select new Village
{
AllianceName = alliance.Element("AllianceName").Value
};
// TODO: make it work...
return alliances.Distinct(new AllianceComparer());
}
catch (Exception ex)
{
throw new Exception("GetAllAlliances", ex);
}
}
Run Code Online (Sandbox Code Playgroud)
由于默认的比较器不适用于Village对象,我实现了一个自定义的比较器,如AllianceComparer类中所示:
public class AllianceComparer : IEqualityComparer<Village>
{
#region IEqualityComparer<Village> Members
bool IEqualityComparer<Village>.Equals(Village x, Village y)
{
// Check whether the compared objects reference the same data.
if (Object.ReferenceEquals(x, y)) …Run Code Online (Sandbox Code Playgroud) 我有一个LINQ Distinct()语句,它使用我自己的自定义比较器,如下所示:
class MyComparer<T> : IEqualityComparer<T> where T : MyType
{
public bool Equals(T x, T y)
{
return x.Id.Equals(y.Id);
}
public int GetHashCode(T obj)
{
return obj.Id.GetHashCode();
}
}
...
var distincts = bundle.GetAllThings.Distinct(new MyComparer<MySubType>());
Run Code Online (Sandbox Code Playgroud)
这一切都很好,花花公子,按我的意愿工作.出于好奇,我是否需要定义自己的Comparer,还是可以用委托替换它?我以为我应该可以这样做:
var distincts = bundle.GetAllThings.Distinct((a,b) => a.Id == b.Id);
Run Code Online (Sandbox Code Playgroud)
但这不编译.有一个巧妙的伎俩吗?
我有2个相同类型的列表.左侧列表:
var leftList = new List<Person>();
leftList.Add(new Person {Id = 1, Name = "John", Changed = false});
leftList.Add(new Person {Id = 2, Name = "Alice", Changed = false});
leftList.Add(new Person {Id = 3, Name = "Mike", Changed = false});
Run Code Online (Sandbox Code Playgroud)
正确的清单:
var rightList = new List<Person>();
rightList.Add(new Person {Id = 1, Name = "John", Changed = false});
rightList.Add(new Person {Id = 3, Name = "Mike", Changed = true});
rightList.Add(new Person {Id = 4, Name = "Joshi", Changed = true}); …Run Code Online (Sandbox Code Playgroud) 我有以下课程
public class Application
{
public int Id { get; set; }
public int Version { get; set; }
(...)
}
Run Code Online (Sandbox Code Playgroud)
我有以下几点IEnumerable<Application>:
IEnumerable<Application> applications1 = new List<Application>
{
new Application {Id = 1, Version = 1},
new Application {Id = 2, Version = 1},
new Application {Id = 3, Version = 3}
};
IEnumerable<Application> applications2 = new List<Application>
{
new Application {Id = 1, Version = 2},
new Application {Id = 3, Version = 2}
new Application {Id …Run Code Online (Sandbox Code Playgroud) 我有两个列表对象.我将它们组合成一个列表.虽然合并我需要删除重复.TweetID是要比较的字段.
List<TweetEntity> tweetEntity1 = tt.GetTweetEntity(Convert.ToInt16(pno), qdecoded, longwoeid );
List<TweetEntity> tweetEntity2 = tt.GetTweetEntity(Convert.ToInt16(pno), qdecoded);
List<TweetEntity> tweetEntity = tweetEntity1.Concat(tweetEntity2).ToList();
Run Code Online (Sandbox Code Playgroud)
我已将两个列表合并,但无法过滤掉重复项.是否有任何内置函数来删除List <>中的重复项?