2

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;
        }
Useme Alehosaini
  • 2,998
  • 6
  • 18
  • 26
Ankit Kumar
  • 476
  • 1
  • 11
  • 38
  • 1
    Does this answer your question? [Explanation of Func](https://stackoverflow.com/questions/878650/explanation-of-func) – Cleptus Dec 22 '20 at 08:43
  • Func Is used as Anonymous delegate in c# which have one return type and and other as parameters of func. Delegate is function pointer that you can use inside function as parameter in your case used two funcs – Jameel Nazir Dec 22 '20 at 09:06

1 Answers1

2

this is the simplest way how func work

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

According to Your Method

public async Task WriteToTable(string lines, DataClass dataclass,
        Func<Dictionary<string, EntityProperty>, string> genPartitionKey,
        Func<Dictionary<string, EntityProperty>, string> genRowKey, bool upsert)

this is your method header and here have two func genPartitionKey,genRowKey

in implementation

 if (HasPartitionAndRowKey(dataclass.TableSchema.Fields))
        {
            genPartitionKey = (Dictionary<string, EntityProperty> props) => props["PartitionKey"].StringValue;
            genRowKey = (Dictionary<string, EntityProperty> props) => props["RowKey"].ToString();
        }

if check is true then he reassign your method genPartitionKey=(Dictionary<string, EntityProperty> props)=>{return props["RowKey"].ToString();}

and same that for the second one

and in this expression he called these both methods var tableRecords =ReadCSV(lines, dataclass.TableSchema.Fields).Select(props => new DynamicTableEntity(genPartitionKey(props), genRowKey(props), string.Empty, props)).ToList();

as I called it

Validate(DependentMethod);

Useme Alehosaini
  • 2,998
  • 6
  • 18
  • 26
Jameel Nazir
  • 134
  • 2
  • 7