Azure表查询 - 子类属性的多个条件

时间:2014-02-26 15:04:21

标签: c# azure azure-table-storage

摘要

  • 我正在尝试使用多个条件查询我的azure表存储。
  • 我的实体对象包含其他子类,我想查询其属性,以及分区键。
  • 根据以下数据模型,我的查询的高级伪代码版本将是:
  • 选择所有MyEventEntity对象WHERE“partitionkey = SOMEVALUE”或“Owner.ID = SOMEVALUE”或“Guests.IDs.ToList()。CONTAINS = SOMEVALUE”。

我的实体数据模型

 public class MyEventEntity : TableEntity
        {
            public Owner Owner { get; set; }
            public List<Guest> Guests { get; set; }

            public MyEventEntity(string partitionKey, string rowKey)
            {
                this.PartitionKey = partitionKey;
                this.RowKey = rowKey;
                this.Guests = new List<Guest>();
            }
        }//End of class.

        public class Guest
        {
            public string ID { get; set; }
            public string FirstName { get; set; }
            public string Surname { get; set; }

            public Guest()
            {

            }

        }//End of class.

        public class Owner
        {
            public string ID { get; private set; }
            public string FirstName { get; set; }
            public string Surname { get; set; }

            public Owner()
            {

            }
        }//End of class.

问题

  1. 如何使用多个过滤器实现这样的查询条件?
  2. 我认为我需要使用TableQuery.CombineFilters函数?
  3. 如何使用此查询方法实际访问子类的属性?
  4. 更新

    • 经过大量阅读后,我编写了三个可能的答案,虽然我们还没能在代码中执行它们,所以它们可能无法正常工作。
    • 我同时感谢任何评论,要么指出语法是否存在问题,要么提出潜在的性能改进建议等。

         var storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
                 var tableClient = storageAccount.CreateCloudTableClient();
                  //var table = tableClient.GetTableReference(tableName);
                  //table.CreateIfNotExists();
                  var tableServiceContext = tableClient.GetDataServiceContext();
               IEnumerable<MyEventEntity> query = from entity in
               tableServiceContext.CreateQuery<MyEventEntity>("myevententities")
                      where entity.PartitionKey.Equals("SomeValue")
                      || entity.Owner.ID.Equals("SomeValue")
                      || entity.Guests.Where(ID => ID == "SomeValue")
                      select entity;
              //////////////////////////////////////////////////////////////////
      
              var storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
              var tableClient = storageAccount.CreateCloudTableClient();
              //var table = tableClient.GetTableReference(tableName);
              //table.CreateIfNotExists();
              var tableServiceContext = tableClient.GetDataServiceContext();
      
               var query = (from entity in tableServiceContext.CreateQuery<MyEventEntity>("myevententities")
                   where entity.PartitionKey.Equals("SomeValue")
                          || entity.Owner.ID.Equals("SomeValue")
                          || entity.Guests.Where(ID => ID == "SomeValue")
                   select new MyEventEntity()).AsTableServiceQuery();
      
              return query.ToList<MyEventEntity>();
      
              //////////////////////////////////////////////////////////////////
      
             var storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
              var tableClient = storageAccount.CreateCloudTableClient();
              //var table = tableClient.GetTableReference(tableName);
              //table.CreateIfNotExists();
              var tableServiceContext = tableClient.GetDataServiceContext();
      
              IEnumerable<MyEventEntity> query = 
                  tableServiceContext.Where(pk => pk.PartitionKey.Equals("SomeValue") 
                  || oID => oID.Owner.ID.Equals("SomeValue")
                  || gID => gID.Guests.Where(ID => ID.Equals("SomeValue")).ToList();
      

    更新2

    • 我想我已经意识到,无论查询方面如何,表存储都不支持我的数据模型......

0 个答案:

没有答案