理解Func的C#方法<;字典<;字符串,EntityProperty>;,字符串>;
我的代码来自另一个源代码,目标是在CSV文件与定义的模式匹配后读取CSV文件,然后将其复制到表格数据库中。在将文件复制到表格数据库之前,CSV文件必须有以下两列:理解Func的C#方法<;字典<;字符串,EntityProperty>;,字符串>;,c#,.net-core,C#,.net Core,我的代码来自另一个源代码,目标是在CSV文件与定义的模式匹配后读取CSV文件,然后将其复制到表格数据库中。在将文件复制到表格数据库之前,CSV文件必须有以下两列:PartitionKey和RowKey。如果分区键不在那里,那么它应该采用作为参数传递的ID。下面是代码,我不明白Funcpart在做什么。有人能给我解释一下它的用途和工作原理吗 //主函数writeTable的调用方式如下: await WriteToTable(lines, dataclass, p
PartitionKey
和RowKey
。如果分区键不在那里,那么它应该采用作为参数传递的ID。下面是代码,我不明白Func
part在做什么。有人能给我解释一下它的用途和工作原理吗
//主函数writeTable的调用方式如下:
await WriteToTable(lines, dataclass,
p => documentId,
p => $"{dataclass.SubType}_{p["RowKey"].Int32Value.Value.ToString("D10")}", upsert);
//写入表格
public async Task WriteToTable(string lines, DataClass dataclass,
Func<Dictionary<string, EntityProperty>, string> genPartitionKey,
Func<Dictionary<string, EntityProperty>, string> genRowKey, bool upsert)
{
const int BatchSize = 100;
if (HasPartitionAndRowKey(dataclass.TableSchema.Fields))
{
genPartitionKey = (Dictionary<string, EntityProperty> props) => props["PartitionKey"].StringValue;
genRowKey = (Dictionary<string, EntityProperty> props) => props["RowKey"].ToString();
}
var tableRecords =ReadCSV(lines, dataclass.TableSchema.Fields)
.Select(props => new DynamicTableEntity(genPartitionKey(props), genRowKey(props), string.Empty, props))
.ToList();
await batchInsertIntoTableStorage(BatchSize,tableRecords, upsert);
}
static readonly string[] RequiredTableKeys = { "PartitionKey", "RowKey" };
private bool HasPartitionAndRowKey(List<TableField> fields)
{
return fields.Select(f => f.Name).Intersect(RequiredTableKeys).Count() == RequiredTableKeys.Length;
}
公共异步任务writeTable(字符串行、数据类、数据类、,
Func genPartitionKey,
Func genRowKey,bool upsert)
{
常量int BatchSize=100;
if(HasPartitionAndRowKey(dataclass.TableSchema.Fields))
{
genPartitionKey=(字典道具)=>props[“PartitionKey”].StringValue;
genRowKey=(字典道具)=>props[“RowKey”].ToString();
}
var tableRecords=ReadCSV(行、dataclass.TableSchema.Fields)
.Select(props=>newdynamictableentity(genPartitionKey(props)、genRowKey(props)、string.Empty、props))
.ToList();
等待batchInsertIntoTableStorage(BatchSize、tableRecords、upsert);
}
静态只读字符串[]RequiredTableKeys={“PartitionKey”,“RowKey”};
private bool HasPartitionAndRowKey(列表字段)
{
返回字段。选择(f=>f.Name)。相交(RequiredTableKeys)。计数()==RequiredTableKeys.Length;
}
这是func工作的最简单方式
public bool Validate(Func<string,bool> dependentMethod)
{
string parmeter = "after execution inside method";
bool isvalid = dependentMethod(parmeter);
return isvalid;
}
public bool DependentMethod(string input)
{
// process there statement and return out put after your business logics
return true;
}
public void CheckValidation()
{
bool isValid= Validate(DependentMethod);
}
public bool验证(函数依赖方法)
{
字符串参数=“在方法内部执行后”;
bool isvalid=依赖方法(参数);
返回有效;
}
公共bool DependentMethod(字符串输入)
{
//处理there语句,并在业务逻辑之后返回
返回true;
}
公共无效检查验证()
{
bool isValid=Validate(DependentMethod);
}
按照你的方法
public async Task WriteToTable(string lines, DataClass dataclass,
Func<Dictionary<string, EntityProperty>, string> genPartitionKey,
Func<Dictionary<string, EntityProperty>, string> genRowKey, bool upsert)
公共异步任务writeTable(字符串行、数据类、数据类、,
Func genPartitionKey,
Func genRowKey,bool upsert)
这是您的方法头,这里有两个func genPartitionKey,genRowKey
实施中
if (HasPartitionAndRowKey(dataclass.TableSchema.Fields))
{
genPartitionKey = (Dictionary<string, EntityProperty> props) => props["PartitionKey"].StringValue;
genRowKey = (Dictionary<string, EntityProperty> props) => props["RowKey"].ToString();
}
if(HasPartitionAndRowKey(dataclass.TableSchema.Fields))
{
genPartitionKey=(字典道具)=>props[“PartitionKey”].StringValue;
genRowKey=(字典道具)=>props[“RowKey”].ToString();
}
如果检查为真,则他会重新分配您的方法
genPartitionKey=(字典道具)=>{return props[“RowKey”].ToString();}
第二个也一样
在这个表达式中,他把这两种方法都称为
var tableRecords=ReadCSV(line,dataclass.TableSchema.Fields)。选择(props=>newdynamictableEntity(genPartitionKey(props),genRowKey(props),string.Empty,props)).ToList()代码>
正如我所说
验证(DependentMethod)代码>这是否回答了您的问题?Func在c#中用作匿名委托,它有一个返回类型,另一个作为Func的参数。委托是函数指针,您可以在函数内部使用它作为参数。在您的案例中使用了两个函数。请解释与我的代码相关的内容?我希望现在您能理解它