【问题标题】:What is required for a LINQ query to work on an Azure Table?LINQ 查询在 Azure 表上工作需要什么?
【发布时间】:2018-02-12 16:20:47
【问题描述】:

我正在尝试将 Azure 应用服务后端用于我正在编写的移动应用。我希望能够针对除 ID 之外的其他内容查询我的表。这有效:

IMobileServiceTable<NewSite> SiteTable = client.GetTable<NewSite>();

Task.Run(async () =>
{
    try
    {
        List<NewSite> items = await SiteTable
            .Where(i => i.Id == "2f2a098a-3b29-4d63-8c03-96869533c034")
            .ToListAsync();

        foreach (var site in items)
        {
            Console.WriteLine(string.Format("{0} : {1}", site.Name, site.SiteID));
        }
    }
    catch(Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }
});

当我尝试查询不同的字段时,即 SiteID,它是这样的字符串:

IMobileServiceTable<NewSite> SiteTable = client.GetTable<NewSite>();

Task.Run(async () =>
{
    try
    {
        List<NewSite> items = await SiteTable
            .Where(i => i.SiteID == "ChIJlx4_GaWaYogRs3NXTsPogTc")
            .ToListAsync();

        foreach (var site in items)
        {
            Console.WriteLine(string.Format("{0} : {1}", site.Name, site.SiteID));
        }
    }
    catch(Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }
});

我得到错误:

DllImport 加载的库 '/system/lib/liblog.so'.Microsoft.WindowsAzure.MobileServices.MobileServiceInvalidOperationException:请求无法完成。 (错误请求)

为了查询数据表中的不同字段,您有什么特别需要做的吗?

谢谢, 吉姆

【问题讨论】:

  • Here 是一些类似的问题。
  • GUID 是否正确?
  • @jdweng 都是字符串,即使 Id 是从 Guid 派生的。
  • @Alex 如果我将 SiteID 小写,那么它将无法编译,因为我的对象是大写的。
  • 在这里查看stackoverflow.com/questions/31743568/… 看起来也很相似

标签: c# linq azure


【解决方案1】:

为了查询数据表中的不同字段,您是否需要做一些特别的事情?

根据您的错误消息,您的项目似乎没有很好地加载库。或者库版本不正确。您可以右键单击项目>管理 Nuget 包>重新安装 Microsoft.Azure.Mobile.Client 包以重试。我也找了一个类似的issue,你可以参考一下。

我已经测试了您的代码,即使我为该字段添加 JsonProperty 属性,我也可以获得结果。

我不确定这个属性的用途是什么,但如果没有它似乎效果会更好。

JsonProperty属性用于定义客户端类型与表的PropertyName映射关系。例如,如果您将 SiteID 的属性名称更改为“siteid”,那么您会得到类似“siteid=id1”的结果,而不是“SiteID=id1”。有关此属性的更多详细信息,您可以阅读此article

【讨论】:

  • 我尝试按照您的要求做,但它仍然对我不起作用。当我使 Json 属性与属性的大小写匹配时,它确实有效。一旦我这样做了,查询就起作用了。
【解决方案2】:

我按照示例应用程序中提供的模板进行操作,但这似乎是问题所在。一旦我删除了 [JsonProperty(PropertyName = "siteid")],它就允许我在该字段上进行查询。我不确定这个属性的用途是什么,但如果没有它似乎效果更好。

public partial class NewSite
{
    public string Id { get; set; }

    //[JsonProperty(PropertyName = "name")]
    public string Name { get; set; }

    //[JsonProperty(PropertyName = "siteid")]
    public string SiteID { get; set; }

    //[JsonProperty(PropertyName = "address")]
    public string Address { get; set; }

    //[JsonProperty(PropertyName = "note")]
    public string Note { get; set; }

    //[JsonProperty(PropertyName = "icon")]
    public string Icon { get; set; }

    //[JsonProperty(PropertyName = "lat")]
    public double? Lat { get; set; }

    //[JsonProperty(PropertyName = "lng")]
    public double? Lng { get; set; }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-09
    • 1970-01-01
    • 1970-01-01
    • 2011-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多