/// <summary>
 /// Load List of all UserDevice objects from Context for Partition Key.
 /// </summary>
 /// <param name="p_Partition">Partition key of the table</param>
 /// <returns>List of all UserDevice </returns>
 public List<UserDevice> GetAllForPartition( string PartitionKey )
 {
     List<UserDevice> rez = new List<UserDevice>();
     NextRecordInfo nextRec = new NextRecordInfo() ;
         do
         {
             DataServiceQuery<UserDevices> Qtran = (DataServiceQuery<UserDevices>)servCtx.CreateQuery<UserDevices>(TableName).Where(c => c.PartitionKey == PartitionKey);
            	nextRec = GetNextPage(ref rez, Qtran, nextRec, 0);
         }
         while (!string.IsNullOrEmpty(nextRec.NextPartition));
     return rez;
 }
        /*

        //   This is an example of custom developed query
        public List<UserLocation> MethodName(list of method parameters )
        {
            List<UserLocation> rez = new List<UserLocation>();
            NextRecordInfo nextRec = new NextRecordInfo() ;
                do
                {
                    DataServiceQuery<UserLocations> Qtran = (DataServiceQuery<UserLocations>)servCtx.CreateQuery<UserLocations>(TableName).Where(c => enter query details here );
                   	nextRec = GetNextPage(ref rez, Qtran, nextRec, 0);
                }
                while (!string.IsNullOrEmpty(nextRec.NextPartition));
            return rez;
        }

        */
        //Add your extension methods here for  UserLocationContext class.
        public List<UserLocation> GetProximityUsers(double latitude , double longitude)
        {
            List<UserLocation> rez = new List<UserLocation>();
            RadiusBox radBox = RadiusBox.Create(latitude, longitude, PROXIMITY_RADIUS);

            NextRecordInfo nextRec = new NextRecordInfo() ;
                do
                {
                    //DataServiceQuery<UserLocations> Qtran = (DataServiceQuery<UserLocations>)servCtx.CreateQuery<UserLocations>(TableName).Where(c => c.PartitionKey <= topline
                    //                                                                                            && double.Parse(c.PartitionKey) >= radBox.BottomLine
                    //                                                                                            &&  double.Parse(c.RowKey) <=radBox.RightLine
                    //                                                                                            && double.Parse(c.RowKey) >=radBox.LeftLine);

                    DataServiceQuery<UserLocations> Qtran = (DataServiceQuery<UserLocations>)servCtx.CreateQuery<UserLocations>(TableName).Where(c => c.PartitionKey.CompareTo(radBox.TopLine.ToString()) < 0
                                                                                                              && c.PartitionKey.CompareTo(radBox.BottomLine.ToString()) > 0
                                                                                                              && c.RowKey.CompareTo(radBox.RightLine.ToString()) > 0
                                                                                                              && c.RowKey.CompareTo(radBox.LeftLine.ToString()) < 0);

                   	nextRec = GetNextPage(ref rez, Qtran, nextRec, 0);
                }
                while (!string.IsNullOrEmpty(nextRec.NextPartition));
            return rez;
        }
 /// <summary>
 /// Load  next page of UserDevice objects from Context for the passed in query and
 /// adds this page to passed in lis.
 /// </summary>
 /// <param name="list">List results will be added to.</param>
 /// <param name="Query">DataServiceQuery object</param>
 /// <param name="nextRec"> NextRecordInfo object define begining of the page to load</param>
 /// <param name="PageSize">Size of the page.  If value is 0  or value greater than 1000 system will limit paze size to 1000 records  </param>
 /// <returns> NextRecordInfo object for the next page</returns>
 private NextRecordInfo GetNextPage(ref List<UserDevice> list, DataServiceQuery<UserDevices> Query, NextRecordInfo nextRec, int PageSize)
 {
     NextRecordInfo Result = new NextRecordInfo();
        QueryOperationResponse qor;
        if (!string.IsNullOrEmpty(nextRec.NextPartition))
        Query = Query.AddQueryOption("NextPartitionKey", nextRec.NextPartition).AddQueryOption("NextRowKey", nextRec.NextKey);
        if (PageSize > 0)
        qor = (QueryOperationResponse)((DataServiceQuery<UserDevices>)Query.Take(PageSize)).Execute();
        else
        qor = (QueryOperationResponse)(Query).Execute();
        string nextPartition = String.Empty;
        string nextRow = String.Empty;
        qor.Headers.TryGetValue("x-ms-continuation-NextPartitionKey", out nextPartition);
        qor.Headers.TryGetValue("x-ms-continuation-NextRowKey", out nextRow);
        IQueryable<UserDevices> qt = (IQueryable<UserDevices>)qor.AsQueryable();
        Result.NextKey = nextRow;
        Result.NextPartition = nextPartition;
        	   var enumer = qt.GetEnumerator();
        while (enumer.MoveNext())
     list.Add( new UserDevice(enumer.Current));
        return Result;
 }