我正在使用 iPhone APP 获取重复的电话号码,我已经编写了代码,但性能太差而且需要时间。这是我的代码:
OrderedDictionary persons = new OrderedDictionary ();
ABAddressBook ab = new ABAddressBook ();
foreach (ABPerson p in ab.GetPeople()) {
foreach (var phoneNumber in p.GetPhones().GetValues()) {
var duplicates = SearchByPhoneNumber (ab, phoneNumber);
if (duplicates.Count > 1) {
if (!persons.Contains (phoneNumber)) {
persons.Add (phoneNumber, duplicates);
}
}
}
}
List<ABPerson> SearchByPhoneNumber ( ABAddressBook ab, string phoneNumber)
{
List<ABPerson> duplicatepeople = new List<ABPerson> ();
phoneNumber = Regex.Replace (phoneNumber, "[^0-9]", "");
var people = ab.Where(x=> x is ABPerson).Cast<ABPerson>().Where(x=> x.GetPhones()
.Where(p=> Regex.Replace(p.Value,"[^0-9]", "")==phoneNumber || phoneNumber ==Regex.Replace(p.Value,"[^0-9]", "")).Count() > 0).ToArray();
foreach(ABPerson person in people)
{
if( duplicatepeople.Intersect(person.GetRelatedNames().Cast<ABPerson>()).Count() <= 0)
{
duplicatepeople.Add(person);
}
}
return duplicatepeople;
}