使用 Azure 移动服务从多个 table 中读取数据
Read data from more than one table using Azure Mobile Services
我有 3 个表:
Table:男人
public string Id { get; set; }
[JsonProperty(PropertyName = "firstName")]
public string FirstName { get; set; }
[JsonProperty(PropertyName = "secondName")]
public string SecondName { get; set; }
Table : 汽车
public string Id { get; set; }
[JsonProperty(PropertyName = "Name")]
public string CarName { get; set; }
Table:人车
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 读取此表?
如您的代码所示,您可能知道如何使用 LINQ 查询。
您使用代码访问数据库的方式非常慢(例如,它将导致一个查询 运行 和每辆 man cars 的 LINQ 开销)
最好为您的服务编写一个 API (CustomController) 并使用 LINQ 连接语句来检索您的数据(单个实际查询为 运行)。
搜索 Azure 自定义 API 和 LINQ 连接语句以获取更多信息
我有 3 个表:
Table:男人
public string Id { get; set; }
[JsonProperty(PropertyName = "firstName")]
public string FirstName { get; set; }
[JsonProperty(PropertyName = "secondName")]
public string SecondName { get; set; }
Table : 汽车
public string Id { get; set; }
[JsonProperty(PropertyName = "Name")]
public string CarName { get; set; }
Table:人车
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 读取此表?
如您的代码所示,您可能知道如何使用 LINQ 查询。
您使用代码访问数据库的方式非常慢(例如,它将导致一个查询 运行 和每辆 man cars 的 LINQ 开销)
最好为您的服务编写一个 API (CustomController) 并使用 LINQ 连接语句来检索您的数据(单个实际查询为 运行)。
搜索 Azure 自定义 API 和 LINQ 连接语句以获取更多信息