Parsing a string C# LINQ expression

You have some options:

  1. Do something homegrown, parsing the text and building an Expression Tree. The standard approach to this would be to use a language parser to parse the string (like ANTLR).

  2. Use CodeDOM to compile the query (NOT recommended for a Production environent as this is slow and generates an assembly per compilation which will saturate your AppDomain with assemblies if you do many. Let me stress, don’t go this route if you have any kind of volume – though this is what LINQPad does) –

  3. Use Mono’s compiler which emits MSIL directly (so no assembly per compilation and much faster) – Mono Compiler as a Service (MCS)

  4. Use Dynamic LINQ (has some limitations and restrictions, but basically does what is suggested in point #1 and is nice, lightweight, and has the ability to only allow certain method calls. It parses the text query and builds an Expression Tree from it) –

Leave a Comment