自定义包含列表 <ReferenceObject> c#
Custom Contains for List<ReferenceObject> c#
我正在尝试在列表中使用 List.Contains
我要比较的对象来自 C# 中的服务参考,它们的 Equals 方法不适合我的需要。
我一直在研究 IEquatables 或如何在我 "given" 的对象中覆盖我的 Equals 方法,但我似乎找不到解决方案。
有人知道一种有效的方法吗?
public void FilterNonExisting(List<ActivitiesActivity> commitActivitiesList)
{
// ActivitiesActivity is the object I'm given through a reference
List<int> itemsToDelete = new List<int>();
int commitCount = 0;
foreach (ActivitiesActivity commitItem in commitActivitiesList)
{
if (this.logList.Contains(commitItem)) // this is the part that doesn't work the way I want it to {
itemsToDelete.Add(commitCount);
}
commitCount++;
}
itemsToDelete.Reverse();
foreach (int item in itemsToDelete)
commitActivitiesList.RemoveAt(item);
if (commitActivitiesList.Count == 0)
{
throw new AllCommitedException("All lines had already been committed");
}
您可以使用 LINQ Any
扩展方法编写您自己的 Contains
:
if (this.logList.Any(x => YourCompareMethod(x, commitItem)))
{ }
Any
将检查对 lambda 表达式的任何调用是否会导致 true
。
您的 YourCompareMethod
应如下所示:
private bool YourCompareMethod(CommitItem a, CommitItem b)
{
// do your comparison
}
听起来你只想实现 IEqualityComparer<ActivitiesActivity>
:
public class ActivitiesActivityEqualityComparer
: IEqualityComparer<ActivitiesActivity>
{
public bool Equals(ActivitiesActivity x, ActivitiesActivity y)
{
...
}
public int GetHashCode(ActivitiesActivity activity)
{
...
}
}
然后:
// Reuse this for multiple calls
var equalityComparer = new ActivitiesActivityEqualityComparer();
...
if (this.logList.Contains(commitItem, equalityComparer))
创建相等比较器后,您可以将其用于各种用途,例如以 activity 作为键的字典、Distinct
和 [=14 等 LINQ 操作=],等等
我正在尝试在列表中使用 List.Contains 我要比较的对象来自 C# 中的服务参考,它们的 Equals 方法不适合我的需要。
我一直在研究 IEquatables 或如何在我 "given" 的对象中覆盖我的 Equals 方法,但我似乎找不到解决方案。 有人知道一种有效的方法吗?
public void FilterNonExisting(List<ActivitiesActivity> commitActivitiesList)
{
// ActivitiesActivity is the object I'm given through a reference
List<int> itemsToDelete = new List<int>();
int commitCount = 0;
foreach (ActivitiesActivity commitItem in commitActivitiesList)
{
if (this.logList.Contains(commitItem)) // this is the part that doesn't work the way I want it to {
itemsToDelete.Add(commitCount);
}
commitCount++;
}
itemsToDelete.Reverse();
foreach (int item in itemsToDelete)
commitActivitiesList.RemoveAt(item);
if (commitActivitiesList.Count == 0)
{
throw new AllCommitedException("All lines had already been committed");
}
您可以使用 LINQ Any
扩展方法编写您自己的 Contains
:
if (this.logList.Any(x => YourCompareMethod(x, commitItem)))
{ }
Any
将检查对 lambda 表达式的任何调用是否会导致 true
。
您的 YourCompareMethod
应如下所示:
private bool YourCompareMethod(CommitItem a, CommitItem b)
{
// do your comparison
}
听起来你只想实现 IEqualityComparer<ActivitiesActivity>
:
public class ActivitiesActivityEqualityComparer
: IEqualityComparer<ActivitiesActivity>
{
public bool Equals(ActivitiesActivity x, ActivitiesActivity y)
{
...
}
public int GetHashCode(ActivitiesActivity activity)
{
...
}
}
然后:
// Reuse this for multiple calls
var equalityComparer = new ActivitiesActivityEqualityComparer();
...
if (this.logList.Contains(commitItem, equalityComparer))
创建相等比较器后,您可以将其用于各种用途,例如以 activity 作为键的字典、Distinct
和 [=14 等 LINQ 操作=],等等