Search code examples
c#reflectionexpression-treesiqueryabledynamic-queries

Creating an expression from the string of a property name?


I am trying to create a query based on some JSON, I currently have the JSON parsed into a set of rules, each rule contains the name of the field, the type of comparison (=, > etc) and the value to compare.

The issue I am having is getting it from that rule, to an IQueryable object, I am guessing I need to use reflection and somehow build the expression tree, but I'm not sure on the right approach...

Assuming I have:

public class Order : BaseEntity
{
    public int OrderID{ get; set; }
}

and I have the rule which is:

public class Rule
    {
        public string field { get; set; }
        public Operations op { get; set; }
        public string data { get; set; }
    }

Running it I get:

field = "OrderID"
op = "eq"
data = "123"

I have the method to parse it with the signature:

public IQueryable<T> FilterObjectSet<T>(IQueryable<T> inputQuery) where T : class

As part of this method I want to do:

inputQuery = inputQuery.Where(o => propertyInfo.Name == rule1.data);

This doesn't work because it basically just generates the sql "OrderID" = "123" which is obviously wrong, I need it to take the column name from inputQuery that has the same name as propertyInfo.Name and build the query that way...

Hope that made sense? Any suggestions?

Edit: I guess what I am asking is to convert a string (Because I can build one pretty simply from the rule) to an expression, maybe using Dynamic LINQ?


Solution

  • In the end I used a Dynamic Linq library I found on Guthrie's Blog:

    http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

    Using this I was able to properly parse out and use the parameters I had built into the rules