Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to query table storage from .net core?

I'm attempting to query table cache using LINQ:

public static IEnumerable<DocumentMetaDataEntity> Get(string connectionString, string cacheName, DeconstructedFileName deconstructedFileName, FileMetaDataFilters filters)
{
    var acc = CloudStorageAccount.Parse(connectionString);
    var tableClient = acc.CreateCloudTableClient();
    var table = tableClient.GetTableReference(cacheName);
    var translations = from entity in table.CreateQuery<DocumentMetaDataEntity>()
                       where (entity.sourceParty == sourceParty.ToLowerTrim()
                                   && entity.destinationParty == destinationParty.ToLowerTrim())
                             || (entity.sourceParty == "YES"
                                   && entity.destinationParty == destinationParty.ToLowerTrim())
                       select entity;

    return translations.Where(x => x.expireAt > DateTime.Now)
                       .Where(x => x.effectiveAt < DateTime.Now);
}

However, getting this exception:

'CloudTable' does not contain a definition for 'CreateQuery' and no accessible extension method 'CreateQuery' accepting a first argument of type 'CloudTable' could be found (are you missing a using directive or an assembly reference?)

What am I doing wrong? Is it not possible to query table cache from .net core?

enter image description here

For some additinal info, here are the create methods available on this class:

enter image description here

Here are all the dependencies:

enter image description here

I've removed LINQ from the equation, yet still getting the following issue:

enter image description here

And the full source is below:

public static IEnumerable<DocumentMetaDataEntity> Get(string connectionString, string cacheName, DeconstructedFileName deconstructedFileName, FileMetaDataFilters filters)
{
    var acc = CloudStorageAccount.Parse(connectionString);
    var tableClient = acc.CreateCloudTableClient();
    var table = tableClient.GetTableReference(cacheName);
    var query = new TableQuery<DocumentMetaDataEntity>().Where
        (TableQuery.CombineFilters
            (TableQuery.GenerateFilterCondition("FacilityCode", QueryComparisons.Equal, deconstructedFileName.FacilityCode)
               , TableOperators.And
               , TableQuery.GenerateFilterCondition("LastName", QueryComparisons.LessThan, deconstructedFileName.LastName)
            )
        );            

    var entities = table.ExecuteQuery(query).ToList();
}
like image 891
Alex Gordon Avatar asked Jan 24 '19 17:01

Alex Gordon


1 Answers

Currently .net core doesn't support querying using Linq as far as I know. But I know You can do it this

var table = acc.GetTableReference("TableName");

var partitionKey = "PartitionKey";

TableQuery<TableModel> rangeQuery = new TableQuery<TableModel>()
      .Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, partitionKey.ToString()));

var entities = table.ExecuteQuery(rangeQuery).ToList();

EDIT: GitHub issue

EDIT 2: ExecuteQuery extension method

public static IEnumerable<T> ExecuteQuery<T>(this CloudTable table, TableQuery<T> query) where T : ITableEntity, new()
    {
        List<T> result = new List<T>();

        TableContinuationToken continuationToken = null;
        do
        {
            // Retrieve a segment (up to 1,000 entities).
            TableQuerySegment<T> tableQueryResult = table.ExecuteQuerySegmentedAsync(query, continuationToken).Result;

            result.AddRange(tableQueryResult.Results);

            continuationToken = tableQueryResult.ContinuationToken;
        } while (continuationToken != null);

        return result;
    }
like image 50
Adam Małek Avatar answered Sep 27 '22 21:09

Adam Małek