如何优化我的C#并使用一个委托函数而不是两个委托函数
下面是我的方法,它调用两个委托函数,该函数依次返回两个键partition和Row key。生成分区和行键的方法相同,唯一的区别是返回的列表中的哪个字符串作为行键和分区键。我想知道是否有办法优化我下面的方法,使用一个代理函数而不是两个。我该怎么做?(如果可能,有人可以告诉我) 然后定义上传如何优化我的C#并使用一个委托函数而不是两个委托函数,c#,C#,下面是我的方法,它调用两个委托函数,该函数依次返回两个键partition和Row key。生成分区和行键的方法相同,唯一的区别是返回的列表中的哪个字符串作为行键和分区键。我想知道是否有办法优化我下面的方法,使用一个代理函数而不是两个。我该怎么做?(如果可能,有人可以告诉我) 然后定义上传 public async Task UploadNCsvData( Func<Dictionary<string, EntityProperty>, Task<string>&g
public async Task UploadNCsvData( Func<Dictionary<string, EntityProperty>, Task<string>> genPartitionKey,
Func<Dictionary<string, EntityProperty>, Task<string>> genRowKey,
Stream lines, string format, bool upsert, IDataClassifier classifier )
{
//mycode and then finally calling WriteToTable()
await WriteToTable( lines, dataclass,
genPartitionKey,
genRowKey, upsert );
}
public异步任务上传ncsvdata(Func genPartitionKey,
Func genRowKey,
流线、字符串格式、布尔upsert、IDataClassifier分类器)
{
//mycode,然后最后调用writeTable()
等待写入表(行、数据类、,
genPartitionKey,
genRowKey,upsert);
}
//最后是writeTable()
public async Task writeTable(流行、数据类、数据类、,
Func genPartitionKey,
Func genRowKey,bool upsert)
{
常量int BatchSize=100;
if(HasPartitionAndRowKey(dataclass.TableSchema.Fields))
{
genPartitionKey=(Dictionary props)=>Task.FromResult(props[“PartitionKey”].StringValue);
genRowKey=(字典道具)=>Task.FromResult(道具[“RowKey”].ToString());
}
var tableRecords=ReadCSV(行,dataclass.TableSchema.Fields)
.Select(async props=>new DynamicTableEntity(wait-genPartitionKey(props)、wait-genRowKey(props)、string.Empty、props))
.ToList();
等待BatchInsertIntoTableStorage(BatchSize、tableRecords、upsert);
}
私有IEnumerable ReadCSV(流源,IEnumerable cols)
{
使用(TextReader=new StreamReader(source,Encoding.UTF8))
{.....
.......
而(csv.Read())
{
收益率-收益率映射。ToDictionary(
col=>col.Name,
col=>EntityProperty.CreateEntityPropertyFromObject(csv.GetField(col.Type,col.Index));
}
}
}
//这些是被调用的函数,正如您所看到的,方法和参数是相同的,只是返回值在传递哪个字符串时有所不同
internal async Task<string> GeneratePartitionKey( Dictionary<string, EntityProperty> arg)
{
var result =await GenerateKeys(arg);
return result[0].ToString();
}
internal async Task<string> GenerateRowKey( Dictionary<string, EntityProperty> arg )
{
var result = await GenerateKeys( arg );
return result[1].ToString();
}
内部异步任务GeneratePartitionKey(字典参数)
{
var结果=等待生成(arg);
返回结果[0]。ToString();
}
内部异步任务生成器wkey(字典arg)
{
var结果=等待生成(arg);
返回结果[1]。ToString();
}
返回ValueTuple如何
内部异步任务生成器(字典arg)
{
var结果=等待生成(arg);
返回(结果[0].ToString(),结果[1].ToString());
}
用法:
public async Task writeTable(流行、数据类、数据类、,
Func genKeys,bool upsert)
{
...
if(HasPartitionAndRowKey(dataclass.TableSchema.Fields))
{
genKeys=props=>Task.FromResult((props[“PartitionKey”].StringValue,props[“RowKey”].ToString());
}
...
var tableRecords=ReadCSV(行、dataclass.TableSchema.Fields)
.选择(异步道具=>
{
var(partitionKey,rowKey)=等待genkey(props);
返回新的DynamicTableEntity(partitionKey、rowKey、string.Empty、props);
})
.ToList();
...
}
请阅读以了解如何使用
async
-wait
中的Select
显示的代码具有相同的返回类型外观相同Task
“只有返回类型在要传递的字符串中不同。”返回类型没有差异。它们在一个参数上有所不同。您首先使用代理有什么原因吗?如果GenerateKeys
处理相同的输入(它们是),从而生成相同的输出(它们应该是),您可以在一个函数中执行该操作,并同时返回两个字符串。这是一个现有代码,在其中进行一些修改将保留代理,而且…是的,这是错误的措辞,它在类型上没有区别编辑并添加了调用您可以为0
或1
引入一个参数。然后只传递一个函数指针,并使用常量或枚举来区分它们的语义。HasPartitionAndRowKey in WritToTable中的行genPartitionKey和genRowKey如何。我改为“genKeys=(Dictionary props)=>Task.FromResult(props[“PartitionKey”].StringValue.ToString(),props[“RowKey”].ToString());”但它说错误不需要两个重载results@ZZZSharePoint缺少一对括号:Task.FromResult((props[“PartitionKey”].StringValue.ToString(),props[“RowKey”].ToString())代码>我扩展了我的答案,也包括了那篇文章。
public async Task WriteToTable( Stream lines, DataClass dataclass,
Func<Dictionary<string, EntityProperty>, Task<string>> genPartitionKey,
Func<Dictionary<string, EntityProperty>, Task<string>> genRowKey, bool upsert )
{
const int BatchSize = 100;
if( HasPartitionAndRowKey( dataclass.TableSchema.Fields ) )
{
genPartitionKey = ( Dictionary<string, EntityProperty> props ) => Task.FromResult(props["PartitionKey"].StringValue);
genRowKey = ( Dictionary<string, EntityProperty> props ) => Task.FromResult(props["RowKey"].ToString());
}
var tableRecords = ReadCSV( lines, dataclass.TableSchema.Fields )
.Select( async props => new DynamicTableEntity( await genPartitionKey( props ), await genRowKey( props ), string.Empty, props ) )
.ToList();
await BatchInsertIntoTableStorage( BatchSize, tableRecords, upsert );
}
private IEnumerable<Dictionary<string, EntityProperty>> ReadCSV( Stream source, IEnumerable<TableField> cols )
{
using( TextReader reader = new StreamReader( source, Encoding.UTF8 ) )
{.....
.......
while( csv.Read() )
{
yield return map.ToDictionary(
col => col.Name,
col => EntityProperty.CreateEntityPropertyFromObject( csv.GetField( col.Type, col.Index ) ) );
}
}
}
internal async Task<string> GeneratePartitionKey( Dictionary<string, EntityProperty> arg)
{
var result =await GenerateKeys(arg);
return result[0].ToString();
}
internal async Task<string> GenerateRowKey( Dictionary<string, EntityProperty> arg )
{
var result = await GenerateKeys( arg );
return result[1].ToString();
}