我有两个列表,并且我试图返回不在另一个列表中的项目。这是我的代码:
line.substring(n)
这大约需要9-10秒才能完成。 ListOne大约有1200条记录,ListTwo大约有33000条记录。
答案 0 :(得分:4)
使用HashSet<T>
(因为它具有O(1)
)的搜索时间可以提高性能,例如
var hashSet = new HashSet<T>(ListTwo.Select(x => Tuple.Create(x.EmployeeNum, x.Sched)));
var results = ListOne.Where(x => !hashSet.Contains(Tuple.Create(x.EmployeeNum, x.Sched)));
答案 1 :(得分:2)
您还可以创建自己的IEqualityComparer(假设您有一个名为Employee
的类):
var results = ListTwo.Except(ListOne, new EmployeeComparer());
IEqualityComparer实现:
public class EmployeeComparer : IEqualityComparer<Employee>
{
public int GetHashCode(Employee co)
{
if (co == null)
{
return 0;
}
return co.EmployeeNum.GetHashCode();
}
public bool Equals(Employee x1, Employee x2)
{
if (object.ReferenceEquals(x1, x2))
{
return true;
}
if (object.ReferenceEquals(x1, null) || object.ReferenceEquals(x2, null))
{
return false;
}
return x1.EmployeeNum == x2.EmployeeNum && x1.Sched == x2.Sched;
}
}
答案 2 :(得分:0)
尝试
set -e