1

Below method gives me error at line "await tableRecords.Add( newprops );" on parameter newprops saying

cannot convert to System.Threading.Task.Task<Microsoft.Azure.Cosmos.Table.DynamicTableEntity>. What wrong am I doing here??

I am trying to add new values in my table records which I am inserting in an Azure table storage.

 public async Task WriteToTable( Stream lines, DataClass dataclass,
               Func<Dictionary<string, EntityProperty>, Task<(string, string)>> genKeys,
               Func<Dictionary<string, EntityProperty>, Task<List<string>>> generateColumns, List<string> columnsList, DynamicTableEntity tableEntry,
               bool upsert )
            {
                const int BatchSize = 100;
                if( HasPartitionAndRowKey( dataclass.TableSchema.Fields ) )
                {
                    genKeys = ( Dictionary<string, EntityProperty> props ) => Task.FromResult( (props["PartitionKey"].StringValue, props["RowKey"].ToString()) );
    
                }
    
                var tableRecords = ReadCSV( lines, dataclass.TableSchema.Fields )
            .Select( async props =>
            {   var (partitionKey, rowKey) = await genKeys( props );
                return new DynamicTableEntity( partitionKey, rowKey, string.Empty, props );
            } ).ToList();
    
                if( columnsList != null )
                {
                    var newColumnValues = ReadCSV( lines, dataclass.TableSchema.Fields )
            .Select( async prop => { await generateColumns( prop ); } ).ToList();
                    var arr = newColumnValues.ToArray();
                    var newprops = new DynamicTableEntity(); 
                    for( int i = 0; i < columnsList.Count; i++ )
                    {
                        newprops.Properties.Add( columnsList[i], EntityProperty.CreateEntityPropertyFromObject( arr[i] ) );
                       await tableRecords.Add( newprops );
                    }
                    
    
                    await BatchInsertIntoTableStorage( BatchSize, tableRecords, upsert );
                }
    
                await BatchInsertIntoTableStorage( BatchSize, tableRecords, upsert );
    
            } 
Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61
ZZZSharePoint
  • 1,163
  • 1
  • 19
  • 54

1 Answers1

2

You do not need to await the tableRecords.Add(newprops);. Awaiting is meant for async functions while

Your tableRecords are a list already:

var tableRecords = ReadCSV( lines, dataclass.TableSchema.Fields )
            .Select( async props =>
            {   var (partitionKey, rowKey) = await genKeys( props );
                return new DynamicTableEntity( partitionKey, rowKey, string.Empty, props );
            } ).ToList();

Change await tableRecords.Add( newprops ); to tableRecords.Add( newprops ); and you'll be fine.

Basically what your error says is that it can't await void

public void Add (T item);

It needs to await a Task and that's not what add returns.

Wait for select

All the above is valid, but you also need break your select and await. WhenAll are done, create your list.

var tasks = await Task.WhenAll(ReadCSV( lines, dataclass.TableSchema.Fields)
            .Select( async props = await genKeys( props )));
var tableRecords =  tasks.Select((partitionKey, rowKey) => new DynamicTableEntity(partitionKey, rowKey, string.Empty, props )).ToList();

Async await in linq select

Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61