Home > Blockchain >  C# Validate string if it's linq expression with NugGet Library
C# Validate string if it's linq expression with NugGet Library

Time:10-25

The string linq query can be entered dynamically, not only by developer. And it should be validated.

Do you have a regex expression or nuget to check if a string is a valid linq expression?

For example for a valid string linq expression :

(a.AccountId== 33
|| a.AccountId == 2
|| a.AccountId == 15) &&
(a.RoleId == 1||  a.RoleId == 3||  a.RoleId == 4)
  • Note - It can be also: (It can be each time a different linq query)
(a.TransactionId == 5 && a.EmployeeName == "Tony") || etc..

Example for string that can't be parsed:

(a.AccountId== 33
|| a.AccountId == 2
|| a.AccountId == 15)
(a.a.RoleId == 1||  a.RoleId == 3||  a.RoleId == 4)

Between those 2 , There is no Operator and you can see the a.a. which is invalid and can't be parsed to linq.

15)
(a.a.RoleId == 1||

If someone knows a good C# NuGet like ExpressionEvaluator for .net framework 4.5, Please let me know.

I have used ExpressionEvaluator, Dot net framework 4.5, But it says that this is a valid linq but it's not:

(a.AccountId== 33
|| a.AccountId == 2
|| a.AccountId == 15)
(a.a.RoleId == 1||  a.RoleId == 3||  a.RoleId == 4)

success is true, but it should be false.

success = new CompiledExpression(query);
  • Result of a NuGet or Regex

Inside the textbox, Will return Valid:

"(a.ProfileId == 5 && a.ProfileName == 'Alex')"

Invalid: - l.l double l.l is invalid

"l.l.LinkId == 5 || l.LinkName == 'Alex'"

valid and string can be parsed

"l.LinkId == 5 || l.LinkName == 'Alex'"

The parameter l. or a. can be changed to any A-Z I have tried to use DynamicExpresso.Core but i need to specify the parameter.

CodePudding user response:

I have used Dynamic Express library, created a dynamic class with property name and property class, passed the right x => arrow function parameter and my query and they dynamic class and it worked. Solved with: https://www.nuget.org/packages/DynamicExpresso.Core/

CodePudding user response:

If I understand it correctly you are searching for a LINQ query that uses the "IN" keyword (SQL). Instead, you can use Contains in LINQ.

.NET:

List<int> number = new List<int>{33,2,15};
from p in a
where p.AccountId.Contains(number)
select p
  • Related