PlTaylor
PlTaylor

Reputation: 7515

Is there a faster way than Compute to do the query data in a DataTable?

I am currently calculating several columns of data using a function similar to this one.

DataTable data = RetrieveDataTable();
var value = data.Compute(
    "AVG(Important_Variable)", 
    "(Distance > 1230 and Distance < 1760) OR (Distance > 4710 and Distance < 5400)"
);

This is working but it is slow. Is there a faster way to achieve the same thing? Different data types are perfectly ok, I am not married to DataTables.

Upvotes: 1

Views: 347

Answers (2)

granadaCoder
granadaCoder

Reputation: 27852

  1. Map everything to a POCO object.
  2. Write a (readonly) get property......that does the calculation.

Here is a ~basic~ ORM mapper (Aka, create your POCO's)

Why is DataTable faster than DataReader

Tack on the readonly (get; only) property that uses the other properties of the object.

If you calculation is "costly", and you read it more than one time, you can use this "nullable" trick.

public class Employee
{

    public DateTime BirthDate { get; set; }
    public DateTime HireDate { get; set; }

    TimeSpan? _difference = null;
    public TimeSpan Difference
    {
        get
        {
            TimeSpan returnValue;
            if (this._difference.HasValue)
            {
                returnValue = this._difference.Value;
            }
            else
            {
                   /* COSTLY CALCULATION HERE , ONE TIME */
                this._difference = this.HireDate.Subtract(this.BirthDate);
                   /* End COSTLY Calculation */

                returnValue = this._difference.Value;
            }
            return returnValue;
        }
    }

}

Upvotes: 1

System Down
System Down

Reputation: 6260

I don't know how you're populating your DataTable but one way that just occurred to me is to accumulate the sum and count of the elements (which you need to calculate an average) as you populate the DataTable. So something like:

int sum;
int count;

for(something) //Loop that populates the data table
{
   .
   .
   var Important_Variable = Read something
   Populate(Important_Variable)
   count++;
   if((Distance > 1230 and Distance < 1760) || (Distance > 4710 and Distance < 5400))
      sum += Important_Variable;
   .
   .
}
var avg = sum/count;

This way you don't have to run the filter after the fact(which is I'm pretty sure is the time consuming part).

Upvotes: 2

Related Questions