【问题标题】:Azure Easy Tables - Load only one columnAzure Easy Tables - 仅加载一列
【发布时间】:2018-03-02 03:33:15
【问题描述】:

有什么方法可以从 Azure Easy Tables 中只获取一行的一个数据列吗?

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

【问题讨论】:

    标签: azure xamarin xamarin.forms


    【解决方案1】:

    这是一个示例,我们只想从 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());
    
            }
        }
    }
    

    【讨论】:

    • 感谢这个作品!但是是否可以将它作为文本保存到字符串中,因为我只想得到一个结果,而 List 正在返回:Microsoft.WindowsAzure.MobileServices.QueryResultList`1[System.String]。一个
    • 嘿@AdamSoták - 没问题,我更新了答案!我为模型添加了默认属性(这些属性由 EasyTables 自动添加),添加了一个名为 GetDogName 的方法,该方法在 Dog 表中为一行返回一列,并修复了几个可能导致编译错误的拼写错误。如果它有效,请给答案一个upvote!非常感谢!
    • 这行得通!谢谢 !我赞成您的回答,但我的 Stack Overflow 声誉低于 15,因此不会显示。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-25
    • 1970-01-01
    相关资源
    最近更新 更多