Home > OS >  How to add more info in the results from a query on cosmos db using a dictionary?
How to add more info in the results from a query on cosmos db using a dictionary?

Time:02-16

I have some data stored in Comsos db and some other in SQL. The data from SQL is retrieved every 5 minutes and is stored in a ConcurrentDictionary. I need to query the data on Cosmos and add more info to the results using the dictionary. So here is the code:

public IAsyncEnumerable<LogDto> GetLogsAsync(string tenantId, CancellationToken cancellationToken)
        {
            return _container.GetItemLinqQueryable<Log>()
                .Where(l => l.TenantId != null && l.TenantId.ToLower() == tenantId.ToLower()) 
                .Take(10)
                .Select(l => new LogDto()
                {
                    SerialId = l.SerialId,
                    SiteId = l.SiteId,
                    SiteName = _cacheService._siteDictionary[l.SiteId],
                })
                .ToFeedIterator()
                .ToAsyncEnumerable(cancellationToken);
        }

But I get the following error:

The specified query includes 'member indexer' which is currently not supported.

Is there any workaround for this? Any help is greatly appreciated.

CodePudding user response:

You'll need to run the query first, then iterate over the resulting collection and set the SiteName property once each item is in-memory since you can't do this as part of the Cosmos query.

Try something like this:

public async IAsyncEnumerable<LogDto> GetLogsAsync(string tenantId, CancellationToken cancellationToken)
{
   var results = _container.GetItemLinqQueryable<Log>()
                .Where(l => l.TenantId != null && l.TenantId.ToLower() == tenantId.ToLower()) 
                .Take(10)
                .Select(l => new LogDto()
                {
                    SerialId = l.SerialId,
                    SiteId = l.SiteId
                })
                .ToFeedIterator()
                .ToAsyncEnumerable(cancellationToken);

   await foreach (var result in results)
   {
      result.SiteName = _cacheService._siteDictionary[result.SiteId];
      yield return result;
   }
}
  • Related