Azure Easy Tables - 仅加载一列

问题描述 投票:2回答:1

有没有办法从Azure Easy Tables中只获取一行的一个数据列?

例如,Xamarin.Forms应用程序将项目的名称发送到Azure并仅获取项目创建DateTime。

azure xamarin xamarin.forms
1个回答
2
投票

这是一个例子,我们想从Name表中选择Dog列。

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

Model

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

Logic

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());

        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.