2015-04-27 129 views
2

我有3個表:讀取數據

表:人

public string Id { get; set; } 
    [JsonProperty(PropertyName = "firstName")] 
    public string FirstName { get; set; } 

    [JsonProperty(PropertyName = "secondName")] 
    public string SecondName { get; set; } 

表:汽車

public string Id { get; set; } 

    [JsonProperty(PropertyName = "Name")] 
    public string CarName { get; set; } 

表:ManCar

public string Id { get; set; } 

    [JsonProperty(PropertyName = "manId")] 
    public string ManId { get; set; } 

    [JsonProperty(PropertyName = "carId")] 
    public string CarId { get; set; } 
C#從Azure代碼讀取日期:
public async Task<IEnumerable<String>> GetAllCarsByMan(Man man) 
    { 
     MobileServiceCollection<ManCar, ManCar> data; 
     IMobileServiceTable<ManCar> dataTable = App.MobileService.GetTable<ManCar>(); 
     try 
     { 
      data = await dataTable 
       .Where(item => item.ManId == man.Id).Select(menuItem => menuItem.CarId) 
       .ToCollectionAsync(); 
     } 
     catch (MobileServiceInvalidOperationException ex) 
     { 
      Tools.errorList.AddMessage("AzureDataProvider.LoadData" + ex.ToString()); 
     } 

     return data; 
    } 


public async Task<IEnumerable<Car>> GetAllCarsForAllMen() 
    { 
     List<Car> listOfCars = new List<Car>(); 
     MobileServiceCollection<Man, Man> data; 
     IMobileServiceTable<Man> dataTable = App.MobileService.GetTable<Man>(); 
    try 
     { 
      data = await dataTable.Where.ToCollectionAsync(); 

      foreach (var man in data) 
      { 
       var cars = await GetAllCarsByMan(Man man); 

       if (cars.Count>0) 
       { 
       IMobileServiceTable<Car> dataCar = App.MobileService.GetTable<Car>(); 
        foreach (var car in cars) 
        { 
         data = await dataTable.Where(item => item.Id == car).ToCollectionAsync(); 
         if(data[0]!=null) 
         { 
         listOfCars.Add(data[0]); 
         } 
        } 
       }      
      } 
     } 
     catch (MobileServiceInvalidOperationException ex) 
     { 
      Tools.errorList.AddMessage("AzureDataProvider.LoadData" + ex.ToString()); 
     } 

     return listOfCars; 
    } 

public async Task<IEnumerable<String>> GetAllCarsByMan(Man man) 
    { 
     MobileServiceCollection<ManCar, ManCar> data; 
     IMobileServiceTable<ManCar> dataTable = App.MobileService.GetTable<ManCar>(); 
     try 
     { 
      data = await dataTable 
       .Where(item => item.ManId == man.Id).Select(menuItem => menuItem.CarId) 
       .ToCollectionAsync(); 
     } 
     catch (MobileServiceInvalidOperationException ex) 
     { 
      Tools.errorList.AddMessage("AzureDataProvider.LoadData" + ex.ToString()); 
     } 

     return data; 
    } 

當我試圖從這些平板電腦中獲取數據時,它非常緩慢。如何使用AzureMobileServices從此表中讀取數據?

回答

2

如您的代碼所示,您可能知道如何使用LINQ查詢。 你使用代碼來訪問你的數據庫的方式是非常慢(例如,它會導致一個查詢運行和每個人的汽車LINQ開銷)
最好爲您的服務編寫一個API(CustomController)並使用LINQ連接語句來檢索您的數據(要運行的單個實際查詢)。
搜索Azure定製API和LINQ連接語句以獲取更多信息