C# Calculation using Data Table Compute

Thomas Jacob 0 Reputation points
2023-05-04T16:36:11.5966667+00:00

I have been using C# Datatable.Compute function to do calculations.

Example:Dot net version 5.0

Given that , Datatable dt = new Datatable();

var a=4000

var b =24

var str = dt.Compute((a/b).ToString(), string.Empty);

The gen0 garbage collector invocation for this single operation is high.

Could you suggest an alternate function to this?

Regards,

Thomas JacobUntitled

Untitled1

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,470 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Viorel 113.7K Reputation points
    2023-05-04T18:36:35.1+00:00

    Probably you meant dt.Compute( $"{a} / {b}", string.Empty ).

    0 comments No comments

  2. Bruce (SqlWork.com) 59,036 Reputation points
    2023-05-04T19:11:41.95+00:00

    its a rather heavy operation. the expression must be parsed, converted to il code, then loaded as an assembly.

    note: if lambda existed, when designed, than they probably would have been used, and the expression compiled at build instead of runtime.

    0 comments No comments