Daniel
Daniel

Reputation: 69

datatable filter with two conditions

I am working with a large datatable. And I need to do some filter to calculate the sum value for certain rows. For the following code, it filters the same IDfile and calculate the sum. My question is if I want to add an other condition combined with IDfile

for (int i = 0; i < LoadIDcount; i++)
{
    string IDnow = LoadID[i, 0];

    for (int j = 0; j < 8760; j++)
    {
        string colPnow = SP_dt.Columns[j * 2 + 4].ColumnName.ToString();
        double ColP_sum = (double)SP_dt.Compute(String.Format("Sum([{0}])", colPnow), String.Format("Load_ID = '{0}'", IDnow));
        string colQnow = SP_dt.Columns[j * 2 + 5].ColumnName.ToString();
        double ColQ_sum = (double)SP_dt.Compute(String.Format("Sum([{0}])", colQnow), String.Format("Load_ID = '{0}'", IDnow));


        LoadP_dt.Rows[i][j + 2] = ColP_sum;
        LoadQ_dt.Rows[i][j + 2] = ColQ_sum;
        Console.WriteLine("{0}    {1}", i, j);
    }

}

The datatable is like:

SP_ID   Load_ID              CUSTOMER_TYPE  Phase   1/1/2015 0:00   
1234567 11111111111          DOM            ABC     3.8928  
2345678 2222222222           DOM            ABC     1.9608  
3456788 33333333             IND            ABC     3.742   
23242   4444444444           IND            ABC     6.9856  
23890583    55555555555      IND            ABC     1.0628  

I want to add two conditions to the compute to filter both ID and customer type

Upvotes: 0

Views: 787

Answers (1)

EduardoCMB
EduardoCMB

Reputation: 494

Just add your second condition to the filter. Like this:

double ColP_sum = (double)SP_dt.Compute(String.Format("Sum([{0}])", colPnow), String.Format("Load_ID = '{0}' AND Phase = '{1}'", IDnow, 'ABC'));

Here is the documentation for the Compute method.

Upvotes: 1

Related Questions