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

Developer technologies C#
{count} votes

2 answers

Sort by: Most helpful
  1. Viorel 122.5K 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) 77,686 Reputation points Volunteer Moderator
    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

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.