Azure Easy Tables - 仅加载一列

标签 azure xamarin xamarin.forms

是否有某种方法可以从 Azure Easy Tables 中仅获取一行的一个数据列?

例如,Xamarin.Forms 应用程序会将项目名称发送到 Azure,并仅获取项目创建日期时间。

最佳答案

下面是一个示例,我们只想从 Dog 表中选择 Name 列。

此示例使用 Azure Mobile ClientAzure Mobile Client SQL NuGet 包。

型号

using Microsoft.WindowsAzure.MobileServices;
using Newtonsoft.Json;

namespace SampleApp
{
    public class Dog
    {
        public string Name { get; set; }
        public string Breed { get; set; }
        public int Age { get; set; }

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

        [CreatedAt]
        public DateTimeOffset CreatedAt { get; set; }

        [UpdatedAt]
        public DateTimeOffset UpdatedAt { get; set; }

        [Version]
        public string AzureVersion { get; set; }

        [Deleted]
        public bool IsDeleted { get; set; }
    }
}

逻辑

using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;

using Microsoft.WindowsAzure.MobileServices;
using Microsoft.WindowsAzure.MobileServices.Sync;
using Microsoft.WindowsAzure.MobileServices.SQLiteStore;

namespace SampleApp
{    
    public class MobileClientService
    {
        bool isMobileClientInitialized;
        MobileServiceClient mobileClient;

        public async Task<string> GetDogName(string id)
        {    
            await InitializeMobileClient(); 

            var dog =  await mobileClient.GetSyncTable<Dog>().LookupAsync(id);
            var dogName = dog.Name;

            return dogName;
        }

        public async Task<IEnumerable<string>> GetDogNames()
        {    
            await InitializeMobileClient(); 

            var dogNameList =  await mobileClient.GetSyncTable<Dog>().Select(x => x.Name).ToEnumerableAsync();

            return dogNameList;
        }

        async Task InitializeMobileClient()
        {
            if(isMobileClientInitialized)
                return;

            mobileClient = new MobileServiceClient("Your Azure Mobile Client Url");

            var path = Path.Combine(MobileServiceClient.DefaultDatabasePath, "app.db");
            var store = new MobileServiceSQLiteStore(path);
            store.DefineTable<Dog>();
            //ToDo Define all remaining tables

            await MobileServiceClient.SyncContext.InitializeAsync(store, new MobileServiceSyncHandler());

        }
    }
}

关于Azure Easy Tables - 仅加载一列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49028633/

相关文章:

c# - 无论如何,我可以将两个值绑定(bind)到我的 XAML 中吗?

xamarin - com.apple.AuthenticationServices.Authorization 错误代码 1000

azure - 如何抑制警告 "ZipDeploy Validation WARNING: It is recommended to set app setting WEBSITE_RUN_FROM_PACKAGE = 1"

azure - 如果从服务总线触发逻辑应用程序,如何识别我的关联 ID 或标识符

azure - 防止 Azure 关闭我的应用服务

android - "java.exe"以代码 2 退出

c# - Xamarin.forms 共享项目在创建时出错

node.js - 禁止访问 Azure 中的 Node.js 源代码文件

c# - onbackpressed 找不到合适的方法来覆盖 xamarin

c# - 我想将我的 Iphone 连接到 MAC mini,并使用 Xamarin UI 测试从我的 Windows 计算机远程运行测试