Click here to Skip to main content
15,916,941 members
Please Sign up or sign in to vote.
5.00/5 (3 votes)
See more:
Say we have the following:
C#
public List<T> Query<T>(Predicate<T> where)
{
   // do query here
   return list;
}

And we are calling the above like this :
C#
var return = Query<SaleInvoiceRow>(row => row.SerialNumber>200 && row.Code < 1000);

How can we get the expression details so we can query our custom data source?
for example -> [FieldName : SerialNumber, Operator : greaterthan, Value : 200 ] AND [ FieldName : Code, Operator: lessthan, Value : 1000 ]
Posted

How about:

C#
static readonly List<int> data = new List<int>() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

static IEnumerable<int> SelectData(Predicate<int> selector)
{
    return from d in data where selector(d) select d;
}

static void Main(string[] args)
{
    Console.WriteLine("data = {0}", string.Join(",", SelectData(d => d>4)));
}


In this example, it is the int type, but it can be any type you like. In der Predicate delegate, you may access any member of the SaleInvoiceRow instance.

Cheers

Andi
 
Share this answer
 
v4
Comments
Mehdi Gholam 31-Jan-12 22:49pm    
I'm sorry but this is not it, I don't want to select the elements I want to get the expression tree.
Andreas Gieriet 1-Feb-12 2:24am    
Ah, yes, you are right. I interpreted your question in a wrong way.
A starting point might be Expression Trees (C# and Visual Basic).

Define your Query function as IEnumerable<T> Query<T>(Expression<Predicate<T>> whereClause) { ... } and analyze according to the description in the link above.

I must admit, I never did so, but I think this is the way to go.

See my modified example in solution 2.

Cheers

Andi
The 1st solution was not what was asked for. 2nd attempt: ;-)

See http://msdn.microsoft.com/en-us/library/bb397951.aspx[^] as starting point.

C#
static readonly List<int> data = new List<int>() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

static IEnumerable<int> SelectData(Expression<Predicate<int>> selector)
{
    ParameterExpression param = (ParameterExpression)selector.Parameters[0];
    BinaryExpression operation = (BinaryExpression)selector.Body;
    ParameterExpression left = (ParameterExpression)operation.Left;
    ConstantExpression right = (ConstantExpression)operation.Right;

    Console.WriteLine("Decomposed expression: {0} => {1} {2} {3}",
                      param.Name, left.Name, operation.NodeType, right.Value);
    //...

    return from d in data where selector.Compile()(d) select d;
}

static void Main(string[] args)
{
    Console.WriteLine("data = {0}", string.Join(",", SelectData(d=>d>4)));
}


The resulting output is:

Decomposed expression: d => d GreaterThan 4
data = 5,6,7,8,9


You have to make the parsing of the expression far more sophisticated. In the example above, the assumption is that there is exactly one binary operation as body of the lambda expression.

Cheers

Andi
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900