我填写了一些ObservableCollection<Employe>
集合:
// Program.Data.Employees - it is ObservableCollection<Employe>.
Program.Data.Employees.Add(new Employe() { Name="Roman", Patronymic="Petrovich", Surname="Ivanov" });
Program.Data.Employees.Add(new Employe() { Name = "Oleg", Patronymic = "Vladimirovich", Surname = "Trofimov" });
Program.Data.Employees.Add(new Employe() { Name = "Anton", Patronymic = "Igorevich", Surname = "Kuznetcov" });
在我的代码的其他地方,我尝试从此集合中删除一些项目:
// Program.Data.Employees - it is ObservableCollection<Employe>.
Employe x = Program.Data.Employees.First(n => n.Guid == emp.Guid); // x is not null.
Int32 index = Program.Data.Employees.IndexOf(x); // I got -1. Why?
Boolean result = Program.Data.Employees.Remove(x); // I got 'false', and item is not removed. Why?
// But this works fine:
Program.Data.Employees.Clear();
我可以清除收藏,但我无法删除必要的项目。为什么会这样?
Equals
类Employe
方法
public bool Equals(Employe other) {
return
other.Guid == this.Guid &&
String.Equals(other.Name, this.Name, StringComparison.CurrentCultureIgnoreCase) &&
String.Equals(other.Patronymic == this.Patronymic, StringComparison.CurrentCultureIgnoreCase) &&
String.Equals(other.Surname == this.Surname, StringComparison.CurrentCultureIgnoreCase) &&
other.Sex == this.Sex &&
String.Equals(other.Post == this.Post, StringComparison.CurrentCultureIgnoreCase);
}
答案 0 :(得分:3)
我尝试使用以下代码重现您的错误:
class Employee
{
public string Name { get; set; }
public Guid Guid { get; set; }
}
// ...
ObservableCollection<Employee> employees = new ObservableCollection<Employee>();
var guid1 = Guid.NewGuid();
employees.Add(new Employee { Name = "Roman", Guid = guid1 });
employees.Add(new Employee { Name = "Oleg", Guid = Guid.NewGuid() });
var x = employees.First(e => e.Guid == guid1);
var index = employees.IndexOf(x); // index = 0, as expected
var result = employees.Remove(x); // result = true, as expected
它按预期工作。我建议你在var x = ...
设置一个breakpont并检查,如果
First()
确实返回该项然后转到下一行并检查是否正确返回index
。如果result
确实是假的话,最后再次检查。
我发现代码失败的几个可能原因:
x=Program.Data.Employees.First()
和Program.Data.Employees.IndexOf()
ObservableCollection
,而是使用由数据层构建的某些派生类(例如DataServiceCollection
,但这个也应该正常工作)。在这种情况下,请在调试器中检查集合的实际类型收集错误的另一个典型原因是,如果您在迭代集合时尝试删除项目(即在foreach
循环内):但在这种情况下应该抛出异常(并且{{1应该工作正常),所以这只适用于你使用一些实现非标准行为的派生类。
编辑 (作为对您发布IndexOf
方法的回复)
您的Equal
方法存在严重错误:
Equal
应该是
String.Equals(other.Patronymic == this.Patronymic, StringComparison.CurrentCultureIgnoreCase)
... // applies also for following comparisons
此外,如果您使用的是Guid,请考虑仅比较GUID,因为这通常意味着“唯一标识符”,因此它应足以识别某个实体。