gpt4 book ai didi

c# - 创建 Dictionary 的字典以存储在 Azure 表存储中

转载 作者:行者123 更新时间:2023-12-03 02:27:30 24 4
gpt4 key购买 nike

我已经为我的 Azure 表存储创建了一个 DynamicTableEntity

var tableRecords= new DynamicTableEntity( partitionKey, rowKey, string.Empty, props );

List<string> Mycolumns = {a, b,c,d} ;
IEnumerable<object> Myvalues = {r,t,y,i};

现在我要添加另一组属性,假设现在我有一个列列表,其中 *List<string>* IEnumerable<object> 中的类型和值列表 输入。

我现有的表记录是这样生成的:

private IEnumerable<Dictionary<string, EntityProperty>> ReadCSV( Stream source, IEnumerable<TableField> cols )
{
using( TextReader reader = new StreamReader( source, Encoding.UTF8 ) )
{
var cache = new TypeConverterCache();
cache.AddConverter<float>( new CSVSingleConverter() );
cache.AddConverter<double>( new CSVDoubleConverter() );
var csv = new CsvReader( reader,
new CsvHelper.Configuration.CsvConfiguration( global::System.Globalization.CultureInfo.InvariantCulture )
{
Delimiter = ",",
HasHeaderRecord = true,
CultureInfo = global::System.Globalization.CultureInfo.InvariantCulture,
TypeConverterCache = cache
} );
csv.Read();
csv.ReadHeader();
var map = (
from col in cols
from src in col.Sources()
let index = csv.GetFieldIndex( src, isTryGet: true )
where index != -1
select new { col.Name, Index = index, Type = col.DataType }).ToList();

while( csv.Read() )
{
yield return map.ToDictionary(
col => col.Name,
col => EntityProperty.CreateEntityPropertyFromObject( csv.GetField( col.Type, col.Index ) ) );
}
}
}




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

await BatchInsertIntoTableStorage( BatchSize, tableRecords, upsert );

}

如何将其添加到现有的 tableRecords 中?

最佳答案

在初始化 tableRecords 之前,将它们添加到 props 中:

    var tableRecords = ReadCSV( lines, dataclass.TableSchema.Fields )
.Select( async props =>
{
var (partitionKey, rowKey) = await genKeys( rops );
return new DynamicTableEntity( partitionKey, rowKey, string.Empty, props );
} )
.ToList();
var arr = Myvalues.toArray();
var newprops = new dictionary<string, EntityProperty>();
for(int i = 0; i< Mycolums.count;i++)
{
newprops.Add(Mycolums[i], arr[i]);
}
//i dont know if you partitionkey /row keys are include in your props so you fill them
tableRecords.Add(new DynamicTableEntity( partition_Key, row_Key, string.Empty, newprops ));

await BatchInsertIntoTableStorage( BatchSize, tableRecords, upsert );

所以在 linq 中,你可以使用:

        var newprops = Mycolumns.Zip(Myvalues, (k, v) => new { k, v })
.ToDictionary(x => x.k, x => x.v);

而不是:

        var arr = Myvalues.toArray();
var newprops = new dictionary<string, EntityProperty>();
for(int i = 0; i< Mycolums.count;i++)
{
newprops.Add(Mycolums[i], arr[i]);
}

关于c# - 创建 Dictionary<string, EntityProperty> 的字典以存储在 Azure 表存储中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66382193/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com