2

I have a Expression Type variable. It has an expression to filter data. For example, The expression lets SQL get data where LOCATION is 'KOR'.

  • {it => (it.LOCATION == "KOR")}

This expression will be the WHERE clause in a SQL statement. Converting lambda expression to Sql queries looks complicated because there are many different expressions.

How can I achieve this purpose without starting from scratch? Are there any nice libraries?

Added....

I can get a string of a lambda expression by invoking the ToString() method.

  • "it => (((it.LOCATION == \"KOR\") OrElse (it.LOCATION == \"FRA\")) AndAlso (it.Value > 30000))"
  • What's the expected output in terms of SQL query? – Francesco De Lisi May 27 '13 at 09:23
  • The expected output is WHERE LOCATION IN ('KOR', 'FRA') AND Value > 30000. – Sung Am YANG May 27 '13 at 09:49
  • If you're looking for a generic solution you may consider the complexity of building a sql clause tree leading back to nested predicates theory. NHibernate or Entity Framework can help you in a different way. – Francesco De Lisi May 27 '13 at 10:20
  • Instead of supporting all expressions I'm going to limit vocabularies so implementing it'll be not too difficult. Thanks for your opinion. – Sung Am YANG May 27 '13 at 12:36
  • It's all about nested clause. Translate a WHERE it's not difficult, but how will you deal with SELECT..FROM (SELECT..FROM..WHERE) INNER JOIN (SELECT..FROM) WHERE...AND...OR? The predicates have different priorities and different solving orders. Let us know :) – Francesco De Lisi May 27 '13 at 12:40
  • In my case the expression never has those keywords (SELECT, FROM, ...) and the input always goes to a WHERE clause. However, it requires some picky works as well. – Sung Am YANG May 27 '13 at 13:37
  • https://stackoverflow.com/questions/10979017/how-to-convert-lambda-expression-to-sql – Dongdong Dec 29 '20 at 19:25

1 Answers1

0

I'm not completely sure what you are asking for, but have you tried using Linq to Sql Dynamic Linq

John Kraft
  • 6,811
  • 4
  • 37
  • 53