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.

zer0ne
  • 403
  • 1
  • 5
  • 13
  • 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. – Eli Gassert Nov 16 '12 at 19:33
  • what if you have a LINQ expression that throws an exception, what should your method return for that? Is it a valid LINQ expression? This seems to me more like an unsolvable problem. – Stan R. Nov 16 '12 at 19:34
  • 1
    FYI that's not a "Linq" expression, it's a Lamba expression. – asawyer Nov 16 '12 at 19:34
  • 1
    Without some real context this problem is not solvable IMHO – Yahia Nov 16 '12 at 19:35
  • 1
    possible duplicate of [Parsing a string C# LINQ expression](http://stackoverflow.com/questions/3782538/parsing-a-string-c-sharp-linq-expression) – Adi Lester Nov 16 '12 at 19:43
  • @asawyer, I think they are equivalent (http://stackoverflow.com/questions/3914611/comparision-linq-vs-lambda-expression) – zer0ne Nov 16 '12 at 19:44
  • 1
    @zer0ne These are Lambda expressions in a LINQ context. By no means are they restricted to LINQ queries however. – asawyer Nov 16 '12 at 19:47
  • LINQ are extension methods. Lambda are compiler sugar for anonymous methods. You can use Lambda with LINQ methods, or you can use query expressions. – Stan R. Nov 16 '12 at 20:00
  • @asawyer Changed to Lambda expression – zer0ne Nov 16 '12 at 20:03
  • @AdiLester Sure, I'll delete this question in a bit. – zer0ne Nov 16 '12 at 20:38
  • @zer0ne Don't delete it, let it close as duplicate or remain open. – asawyer Nov 16 '12 at 20:40

1 Answers1

-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.

Tomas Grosup
  • 6,396
  • 3
  • 30
  • 44