使用Azure移动服务从多个表中读取数据

时间:2015-04-27 21:25:39

标签: c# linq sql-server-2008 azure azure-mobile-services

我有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; }

来自Azure代码的C#读取日期:

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从这些表中读取?

1 个答案:

答案 0 :(得分:2)

如您的代码所示,您可能知道如何使用LINQ查询。 您使用代码访问数据库的方式非常慢(例如,它将导致一个查询运行和每个人车的LINQ开销)
最好为您的服务编写API(CustomController)并使用LINQ join语句来检索您的数据(要运行的单个实际查询)。
搜索Azure Custom API和LINQ join语句以获取更多信息