I have this code from a different source where the objective is reading a CSV file after it matches a defined schema and then copying it into a tabular database. before copying the file into the tabular database, it is necessary for the CSV file to have these two columns: PartitionKey
and RowKey
. If the partition key is not there then it should take the ID which is being passed as an argument. Below is the code and I don't understand what Func<Dictionary<>,string>
part is doing. Can someone please explain to me what it is being used for and how it works?
// the main function WriteToTable is called like this:
await WriteToTable(lines, dataclass,
p => documentId,
p => $"{dataclass.SubType}_{p["RowKey"].Int32Value.Value.ToString("D10")}", upsert);
//Write To Table
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;
}