Script em c# para obter a soma total de valores de uma coluna em uma tabela

Olá comunidade!

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.

Muito Obrigada pessoal!

Hi @devrpa746

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.

I hope it helps you!!

Na parte do código em que defino a variável com o nome da coluna, a minha tabela não possui cabeçalho, então tem que ser pelo index da coluna.

eu mudo o tipo pra inteiro e faço dessa maneira?

int pathToCsv = 3; ou não?

eu fiz isso mas não me retorna nada como soma, me retorna zero.