Reputation: 7515
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
Reputation: 27852
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
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