Search for contacts by phone number, filter using 3-digit prefix

I want to get all phone numbers in my contacts that start with 3 digit digits like "012" when I press a button.

I worked around this using the following code:

private void ButtonContacts_Click(object sender, RoutedEventArgs e)
{
    Contacts cons = new Contacts();

   //Identify the method that runs after the asynchronous search completes.
   cons.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(Contacts_SearchCompleted);

   //Start the asynchronous search.
   cons.SearchAsync("0109", FilterKind.PhoneNumber, "State String 5");
}


void Contacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
{
    try
    {
        //Bind the results to the user interface.
        ContactResultsData.DataContext = e.Results;
    }
    catch (System.Exception)
    {
        //No results
    }

    if (ContactResultsData.Items.Any())
    {
        ContactResultsLabel.Text = "results";
    }
    else
    {
        ContactResultsLabel.Text = "no results";
    }
}

      

but FilterKind.PhoneNumber

only works when it has at least the last 6 digits corresponding to the phone number.
Any idea how to achieve this?
BTW I'm a beginner.

+3


source to share


1 answer


As you say, the api contacts filter only matches if the last six digits are the same, you can see it in the documentation, so you cannot do it using it.

In my opinion, the best way to do this is to get a list of all contacts and then use LINQ to find the contact you want.



private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
    var contacts = new Contacts();
    contacts.SearchCompleted += Contacts_SearchCompleted;
    contacts.SearchAsync(null, FilterKind.None, null);
}

void Contacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
{
    var results = e.Results.ToArray();
    var myContacts = results.Where(c => c.PhoneNumbers.Any(p => p.PhoneNumber.StartsWith("66"))).ToArray();
}

      

On the last line, you can see a request to find contacts that some of their numbers start with 66. You can modify this request however you want to match the numbers you want.

+1


source







All Articles