I want to write a simple expression language & LINQ to Entities translator for a small DB application. The translator should simply translate expressions to linq queries, e.g.
[Project Participants] > 50 AND [Project Duration in Months] < 4
should translate to a similar LINQ query as
from p in projects where p.Participants.Count > 50 && p.Duration < 4 select p;
dynamically at runtime.
My aim is to store user specific constraints in a database and use these constraints to generate a list of projects on demand using the entity framework. What is a good practice to do such a ‘translation’?
Thanks a lot,
Greetings
My recommendation would be to use ScottGu’s dynamic LINQ libs, found here:
http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx
Basically you can parse your simple expression language as a string and using some YACC-style code build the appropriate LINQ query parts at runtime.