Poderiam me ajudar, fornecendo um script em C# para obter a soma total de valores de uma coluna em uma tabela? Preciso fazer a leitura da coluna da tabela e a soma das linhas invocando um script C#. Sem ter que usar a atividade read csv. Pois estou com problemas ao usar a atividade.
Lembrando que o arquivo que preciso ler é um .CSV separado por virgulas.
I am giving the C# code below. Check for reading the data from csv file and doing sum of the required colmn.
// First, define the path to your CSV file:
string pathToCsv = “your_path_to_csv_file”;
// Next, read the CSV file as text:
string csvText = File.ReadAllText(pathToCsv);
// Split the text into rows:
string rows = csvText.Split(‘\n’);
// Define the name of the column you want to sum:
string columnName = “your_column_name”;
// Initialize the sum variable:
int sum = 0;
// Loop through each row and sum the value in the column:
foreach (string row in rows)
{
// Split the row into columns:
string columns = row.Split(‘,’);
// Find the index of the column you want to sum:
int columnIndex = Array.IndexOf(columns, columnName);
// If the column doesn't exist, skip this row:
if (columnIndex < 0) continue;
// Parse the value in the column as an integer:
int value;
if (!int.TryParse(columns[columnIndex], out value)) continue;
// Add the value to the sum:
sum += value;
}
// Finally, output the sum:
Console.WriteLine("The sum of the column is: " + sum);
In this “your_path_to_csv_file” give the csv file path, and “your_column_name” here give the column name that you want to sum.
Note: Make sure to include the necessary namespace and assembly references in the “Imports” and
“References” sections of the “Invoke Code” activity properties.