Evaluate the string as math expression [duplicate] - c#

This question already has answers here:
Evaluating string "3*(4+2)" yield int 18 [duplicate]
(13 answers)
Is there a string math evaluator in .NET?
(18 answers)
Closed 4 years ago.
I have one scenario in C#, which I fetch the expression from database, I need to parse and evaluate the same
The expression can be of any type (With + & - Operator only) as mentioned below
X+Y-Z
X-Y-Z
Z-Y+Z
Where as at run time I have any of above expression as string item and the values for each variable defined in it.
Now I need to automate the same, so that my code at run time will able to parse and evaluate the same.
I believe Switch case and if/else loop is the one way, but any can please suggest some other better and efficient way.
Thanks in advance

Related

How evaluate a string? [duplicate]

This question already has answers here:
How can I evaluate a C# expression dynamically?
(9 answers)
Closed 3 years ago.
So basically what i have been struggling with is to evaluate sentence in EF Core.
For example if i have:
string condition = "65 < 100 AND 65 > 50"
How can evaluate that?
You can execute SQL String in EF Core, So what you need is to write the whole SQL you want in a sctring and then concatenate this part to it. after that you execute it like this
myDbContextObject.Database.ExecuteSqlCommand(sqlString,params[]);
OR
myDbContextObject.Table.FromSql(sqlString,params[]);

Why do Boolean.TryParse()] and Convert.ToBoolean() evaluate a string differently? [duplicate]

This question already has answers here:
What is the difference between Convert.ToBoolean(string) and Boolean.Parse(string)?
(3 answers)
Closed 6 years ago.
Why do Boolean.TryParse() and Convert.ToBoolean() evaluate a string differently?
I understand how they end up evaluating differently:
Boolean.TryParse() will match (case insensitive) 'true' and 'false'.
Convert.ToBoolean() will match to a whole range of values (example demonstrated in Microsoft doco linked above) which I would consider more natural.
Its the reasoning behind the difference I dont understand.
There are a couple of discussions touching on this subject which don't seem to address this particular question.
It's in the method/class names.
Convert -> you already have some value, you convert it to another type. e.g. you have value 1 which can be converted to true.
Parse -> you have the value as a string and you parse it.

Dynamic String Validation [duplicate]

This question already has answers here:
Serializing and Deserializing Expression Trees in C#
(7 answers)
Is it possible to dynamically compile and execute C# code fragments?
(7 answers)
Closed 7 years ago.
I wonder if it is possible to convert text to C# code for dynamic validation. For example, I'd love to be able to store a string in my DB like this:
((Value.StartsWith("CR") || Value.StartsWith("TRC")) && Value.Length == 10)
Then - pull it from the DB and use it to validate data. If would automatically swap out Value for the piece of data I'm trying to validate.
All validations would return a boolean that I would use in the code to see if the data being tested passed or failed.
I would have numerous variations of validations similar to the one above like this stored in my DB.
I've tried researching various phrases on Google but can't seem to think of what this might be called?

C# how to detect operators in string to calculate? [duplicate]

This question already has answers here:
Is there a string math evaluator in .NET?
(18 answers)
Closed 9 years ago.
Say a program receives an input string "8*10+6/2" and should output 83, in this case. how to handle the operator?
I can chop the string into individual strings, then detect whether it is a number or operator. If it is an operator I can convert it to int. But I have no idea how to handle the operator so that the calculation works.
You could use the DataTable.Compute-"trick":
double result = (double)new DataTable().Compute("8*10+6/2", null);
The following arithmetic operators are supported in expressions:
+ (addition)
- (subtraction)
* (multiplication)
/ (division)
% (modulus)
More informations in: DataColumn.Expression at Expression Syntax.

Equivalence of String and string in C# test [duplicate]

This question already exists:
Closed 11 years ago.
Possible Duplicate:
String vs string in C#
I have a test in C# code I'm reading:
if (variable is string)
I am wondering if this is strictly equivalent to:
if (variable is String)
or if some esoteric behavior of C# autoboxing may cause these tests to behave differently.
They are exactly the same - string is an alias for System.String.

Categories