简体   繁体   English

如何选择 Azure 存储表中缺少字段的记录?

[英]How to select records where field is missing from an Azure Storage Table?

I'm attempting to process records that were inserted into Azure Table Storage prior to the addition of a new attribute.我正在尝试处理在添加新属性之前插入 Azure 表存储的记录。 The LINQ support is limited and I'm struggling to get this to work. LINQ 支持有限,我正在努力让它发挥作用。

How would one use LINQ (or another method) to filter an Azure Table to just records missing an attribute for a given entity?如何使用 LINQ(或其他方法)过滤 Azure 表以仅记录缺少给定实体的属性?

Sample Code示例代码

I'm writing it in an Azure Function, and the row count here returns 0, when attempting a default value for the field.我在 Azure 函数中编写它,当尝试为该字段设置默认值时,此处的行数返回 0。

#r "Microsoft.WindowsAzure.Storage"

using System.Net;
using Microsoft.WindowsAzure.Storage.Table;

public static HttpResponseMessage Run(HttpRequestMessage req, IQueryable<ts_webhit> inTable, TraceWriter log)
{
    IEnumerable<ts_webhit> recs = (from r in inTable
                                   where "2016-11-21 12:45" == r.PartitionKey
                                   &&    ""                 == r.Category  //The filter I need to run
                                   select r);
    log.Info($"{recs.Count()}");
    return req.CreateResponse(HttpStatusCode.OK, recs.ToList());
}

public class ts_webhit : TableEntity
{

    public string Category { get; set; } = ""; //Attempting a default value

}

Some candidate filters that didn't work一些无效的候选过滤器

  • r.Category is nothing generates a compilation error r.Category is nothing产生编译错误
  • String.IsNullOrEmpty(r.Category) returns that it's not supported String.IsNullOrEmpty(r.Category)返回它不受支持
  • r.Category == null returns a bad request r.Category == null返回错误的请求
  • !r.Category.HasValue generates a compilation error as it's a string !r.Category.HasValue生成一个编译错误,因为它是一个字符串

如果该属性在写入表存储时实体上不存在,则该列将不存在于表中,因此您在查询中进行的任何比较 - 包括与空字符串的比较 - 都将失败,您将得到一个空回复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM