csetzkorn csetzkorn - 4 years ago 70
C# Question

fast creation of a calculated column in c#

Let us say I am being given a 'string' formula from another source. Example:

NewCalculatedColumn = (Column1 * Column2)/Column3

I would like to apply this formula to create a calculated column for some data stored as double array (or DataTable - I have freedom here) in memory.

In this particular example, the array/dataset consist of 3 column and has thousands of rows. One option is to use DataColumn.Expressions, if the data is stored in a DataTable, as discussed here. However, this may not be the most efficient way. Any feedback would be very much appreciated. Many thanks!

Answer Source

DataTable is a much heavier data structure than a list of objects (or the more generic IEnumerable<T>), as it is indicated here.

So, if you are not forced into using a DataTable a list of objects that look like the following, can be used:

public ObjectType
    public double Column1 { get; set; }
    public double Column2 { get; set; }
    public double Column3 { get; set; }

    // avoid division by zero, adjust zero comparison threshold as needed
    // also adjust returned value on zero
    // using C# 6.0 specific syntax. If not available, use get { return } syntax 
    public double NewCalculatedColumn => Math.Abs(Column3) > 0.0001 ?  
        (Column1 * Column2)/Column3 
        : 0.0;

Even if you fetch data as DataTable, you can easily convert it to List<ObjectType> as indicated here.


Based on comment, if expression can is dynamic, an external library can be used. E.g. NCalc:

    public double NewCalculatedColumn 
            // you can provide a dynamic expression which contains col1, col2 and col3
            //TODO: add exception handling
            var e = new Expression($"(col1 * col2)/{col3}");
            e.Parameters["col1"] = Column1;
            e.Parameters["col2"] = Column2;
            e.Parameters["col3"] = Column3;
            return e.Evaluate();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download