0

I am using Corodva Contacts Plugin to fetch the local phone contacts its working great,Now i need to compare it with database,I am not using local db, I am using sql server 2012 and I have written some back-end code with WEBAPI.Its taking lot of time to compare. I need some alternative solution to it. Kindly suggest below is my code.

         //Javascript///

  var phoneNumberCollection = new Array();

  function showContacts() {

var options = new ContactFindOptions();
  options.filter = "";
  options.multiple = true;
  options.desiredFields = [navigator.contacts.fieldType.id,navigator.contacts.fieldType.displayName,  navigator.contacts.fieldType.name, navigator.contacts.fieldType.phoneNumbers];
options.hasPhoneNumber = true;
   var fields = [navigator.contacts.fieldType.displayName, navigator.contacts.fieldType.name];
   navigator.contacts.find(fields, onSuccess, onError, options);
}

 function onSuccess(contacts) 
  {
     // here i have all contacts and i am pushing each number into       "phoneNumberCollection" array//
  }

 function onError(err)
 { 
 }

  self.GetContactsData = function () {
    self.PhoneNumberCollection = phoneNumberCollection;
    jQuery.support.cors = true;
    $.ajax({
        type: "POST",
        dataType: "json",
        contentType: 'application/json; charset=utf-8',
        data: JSON.stringify({mobilecollection:  self.PhoneNumberCollection,}),
        url: Url + 'api/xxxxxx/xxxxxxxx',
        success: function (data) {
            self.items($.map(data, function (item) {
                return new ContactsModel(item);
            }));
        },
        error: function (err, type, httpStatus) {
         }
    })

}

     //Web API
  [HttpPost]
    public  IHttpActionResult GetContacts(JObject jsonData)
    {
        try
        {
            if (jsonData != null)
            {
                dynamic json = jsonData;

                string[] mobilenumberCollection = json.mobilecollection.ToObject<string[]>();
    //here i am getting Mobile collection and i am comparing each number with DB. i need some alternative sugggestion for this
   var getContacts = CBFriends.getAllcontacts(mobilenumberCollection, deviceUID);
                if (getAllContacts != null)
                {
                    return Ok(getAllContacts);
                }
                else { return NotFound(); }

            }
            else {
                return BadRequest();}
           }
        catch (Exception)
        { }      
       }
kitty sarvaj
  • 517
  • 5
  • 10
  • 24
  • It's depends on many parameters, which field you check in DB only id or all fields in contacts? – Ygalbel Mar 22 '17 at 16:39
  • @ygalbel Thanks for your time, I have column called Mobile Number in my DB so I need to check only one column, I am comparing only the local phone number with Database Mobile Number kindly suggest. – kitty sarvaj Mar 23 '17 at 06:17

1 Answers1

0

Check IN operator in sql server.

You can send all phone numbers in one query and get ones that not exists in db.

Check this answer, to see how to achieve this in c#.

Community
  • 1
  • 1
Ygalbel
  • 5,214
  • 1
  • 24
  • 32
  • Thanks For your time, I saw that,But i am using Linq to fetch the data,is there any example using Linq to sql – kitty sarvaj Mar 23 '17 at 11:43
  • Check [here](http://stackoverflow.com/questions/2334327/what-is-the-linq-equivalent-to-the-sql-in-operator) – Ygalbel Mar 23 '17 at 11:52
  • var MobileNumberExsitDB = dbContext.UserTable.Where(m => m.mobileNumber == mobilenumber).FirstOrDefault(); i am using query like this, mobilenumber which will in foreach loop contains one number at time – kitty sarvaj Mar 23 '17 at 11:54
  • I need to get user Details related to existing mobilenumber in DB,so i can use for binding – kitty sarvaj Mar 23 '17 at 12:01
  • You have to made one query for all the phone numbers. – Ygalbel Mar 23 '17 at 12:09
  • 1, create an array with all phoneNumbers 2. run query like this: dbContext.UserTable.Where(m => phoneArray.Contains(m)); – Ygalbel Mar 23 '17 at 12:10
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/138818/discussion-between-kitty-sarvaj-and-ygalbel). – kitty sarvaj Mar 23 '17 at 12:10
  • https://github.com/kittysarvaj/Compare-Contacts/blob/master/.gitignore, i have created the new file in github,please suggest me – kitty sarvaj Mar 23 '17 at 13:19
  • var numbersThatNotExistsInDB = mobileNumbers.Where(!usersThatExsitInDB.Any(a => a.mobileNumber == a)).ToList(); i am getting the error ,which say "operand==" cannot applied to string type UserTable – kitty sarvaj Mar 23 '17 at 15:26
  • Sorry, change to `var numbersThatNotExistsInDB = mobileNumbers.Where(!usersThatExsitInDB.Any(a => a.mobileNumber == mobileNumbers)).ToList();` – Ygalbel Mar 23 '17 at 15:38
  • still it didnt work,i think its comparing single value a.mobileNumber with list values – kitty sarvaj Mar 23 '17 at 15:40
  • Fixed. Check `var numbersThatNotExistsInDB = mobileNumbers.Where(n => !usersThatExsitInDB.Any(a => a.mobileNumber == n)).ToList();` – Ygalbel Mar 23 '17 at 15:57