0

For example:

Validate("items.Select(item => item.ToLower())")

will return True, while

Validate("var n=5;")

will return False.

The context is that I want to create an application that allows users to manipulate their data via a lambda expression. If the user inputs a valid lambda expression, the program will execute it. Otherwise, it should fail.

11
  • 2
    What's the context? How will this be used? Why does it need to be a string version of the LINQ expression and not the LINQ expression? Just trying to arrive at the intent so I can better understand a solution. Commented Nov 16, 2012 at 19:33
  • 1
    FYI that's not a "Linq" expression, it's a Lamba expression. Commented Nov 16, 2012 at 19:34
  • 1
    Without some real context this problem is not solvable IMHO Commented Nov 16, 2012 at 19:35
  • 1
    possible duplicate of Parsing a string C# LINQ expression Commented Nov 16, 2012 at 19:43
  • 1
    @zer0ne These are Lambda expressions in a LINQ context. By no means are they restricted to LINQ queries however. Commented Nov 16, 2012 at 19:47

1 Answer 1

-1

I would recommend of keeping it simple and just trying to execute it. If it isnt valid, it will fail anyway a few milliseconds later.

It also deals with expressions that are syntactically valid, but fail from a different reason.

If you choose to take this approach, than it only takes to use the Dynamic Linq library and you are done.

Sign up to request clarification or add additional context in comments.

3 Comments

"it will fail anyway a few milliseconds later" LINQValidate("while(true){var s=\"good luck\"";}");
If he must deal with evil users, he can execute it on a separate thread and kill it if it takes too long.
Ok. LINQValidate("System.Diagnostics.Process.Start(\"cmd format c:\");");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.