Query Parser in C#

370 views Asked by At

I'm making parser which help to parse the data from Query string in API endPoint.

e.g. https://testt.ap.com/testdata/v1/data?fq= ((A AND B) OR (C AND D))

I need to parse the data and pass it to LINQ which will connect with database and get the data.

SQL query which will form from these string where it should do AND with A,B and C,D and after that It will perform the OR.

It's one of the example, User Can pass any complex query to filter the data.

Note : I'm not going to use Odata library.

2

There are 2 answers

0
Samuel Vidal On

You can try using a recursive descent parser, this is pretty straightforward and fun to implement. You can check that link.

0
JasperDaDolphin On

I made something like this cause I couldn't find a very simple one.

Github

The library works in three steps:

  • Parsing the input string into a list of tokens.
  • Building an Abstract Syntax Tree (AST) from the tokens.
  • Generating a Linq Expression from the AST.