在.NET GetHashCode方法中,很多地方都使用.NET 方法.特别是在快速查找集合中的项目或确定相等性时.是否有关于如何GetHashCode为我的自定义类实现覆盖的标准算法/最佳实践,因此我不会降低性能?
使用GroupBy()和Count() > 1我试图在列表中查找我的类的重复实例.
这个类看起来像这样:
public class SampleObject
{
public string Id;
public IEnumerable<string> Events;
}
Run Code Online (Sandbox Code Playgroud)
这就是我实例化和分组列表的方式:
public class Program
{
private static void Main(string[] args)
{
var items = new List<SampleObject>()
{
new SampleObject() { Id = "Id", Events = new List<string>() { "ExampleEvent" } },
new SampleObject() { Id = "Id", Events = new List<string>() { "ExampleEvent" } }
};
var duplicates = items.GroupBy(x => new { Token = x.Id, x.Events })
.Where(g => g.Count() …Run Code Online (Sandbox Code Playgroud) 我想比较两个嵌套对象列表。如果父对象Id不同和/或任何子对象Id或Baz属性不同,我想认为它们已更改。
我已经实现了我自己的版本Equals和GetHashCode以下版本,但尽管使用了我自己的 equalcomparer,Except()仍然会产生结果,而我希望对象是相等的。
var foo1 = new Foo
{
Id = 1,
Bars = new List<Bar>
{
new Bar
{
Id = 1,
Baz = 1.5
},
new Bar
{
Id = 1,
Baz = 1.5
}
}
};
var foo2 = new Foo
{
Id = 1,
Bars = new List<Bar>
{
new Bar
{
Id = 1,
Baz = 1.5
},
new Bar
{
Id = …Run Code Online (Sandbox Code Playgroud)
我只有获得不同的列表有问题.
所以我有 :
List<List<int>> combinations = new List<List<int>>();
combinations.Add(new List<int> {1,1});
combinations.Add(new List<int> {1,2});
combinations.Add(new List<int> {1,1}); // Same
combinations.Add(new List<int> {1,3});
Run Code Online (Sandbox Code Playgroud)
我需要做的只是获得:
{1,1}
{1,2}
{1,3}
Run Code Online (Sandbox Code Playgroud)
我试过这个:combinations = combinations.Distinct().ToList();
但它不起作用.
有任何想法吗.先感谢您.
c# ×3
.net ×1
algorithm ×1
comparison ×1
distinct ×1
gethashcode ×1
grouping ×1
hashcode ×1
linq ×1
list ×1