简体   繁体   中英

Cannot convert Microsoft.Azure.Cosmos.Table.DynamicTableEntity to System.Threading.Task.Task<Microsoft.Azure.Cosmos.Table.DynamicTableEntity>

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 );
    
            } 

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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