繁体   English   中英

无法将 Microsoft.Azure.Cosmos.Table.DynamicTableEntity 转换为 System.Threading.Task.Task <microsoft.azure.cosmos.table.dynamictableentity></microsoft.azure.cosmos.table.dynamictableentity>

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

下面的方法在“await tableRecords.Add(newprops);”行给了我错误关于参数 newprops 说

无法转换为 System.Threading.Task.Task<Microsoft.Azure.Cosmos.Table.DynamicTableEntity>。 我在这里做错了什么??

我正在尝试在我插入 Azure 表存储的表记录中添加新值。

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

您不需要await tableRecords.Add(newprops); . Awaiting 用于async功能,而

您的 tableRecords 已经是一个列表:

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

更改await tableRecords.Add( newprops ); tableRecords.Add( newprops ); 你会没事的。

基本上你的错误说的是它不能等待void

public void Add (T item);

它需要等待一个Task ,而这不是 add 返回的。

等待 select

以上所有内容都是有效的,但您还需要打破您的 select 并等待。 WhenAll完成后,创建您的列表。

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

linq select 中的异步等待

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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