使用Mobile Service客户端从Azure数据库中过滤数据

时间:2018-11-14 14:20:44

标签: forms azure xamarin mobile service

我试图限制Azure门户上托管的Mobile App服务返回的数据。不幸的是,在返回数据之前(在createQuery()阶段),我无法限制数据。该过滤器仅在返回数据后应用时才起作用。这是示例代码。该应用程序是在iOS平台上以Xamarin形式编写的。

     public async Task<IEnumerable<Company>> GetCompanies(string  
                                      cityId)
     {
        await App.MobileService.SyncContext.InitializeAsync(store, 
                         handler);
        this.companies = App.MobileService.GetSyncTable<Company>();

        try
        {
            await App.MobileService.SyncContext.PushAsync();

            // Filter is unsuccessful here
            await this.companies.PullAsync("all_companies", 
              this.companies.CreateQuery().Where(x => x.City_Id ==  
                    cityId)).ConfigureAwait(false);
          }
          catch (MobileServicePushFailedException ex)
          {
               string ErrorString = string.Format("Push failed because 
                 of Company sync errors: {0} errors, message: 
                  {1}",ex.PushResult.Errors.Count, ex.Message);
           }
           catch (Exception ex)
            {
                 Debug.WriteLine("Exception Sync companies: " + ex);
            }
             // Filter works here. But its too late as it has already
             // pulled all the unnecessary data
             return await companies.Where(x => x.City_Id == 
               cityId).OrderBy(c => c.Id).ToEnumerableAsync();
       }

0 个答案:

没有答案