Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# LINQ
Say we have the following:
public List<T> Query<T>(Predicate<T> where)
{
   // do query here
   return list;
}
And we are calling the above like this :
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 30-Jan-12 22:40pm
Mehdi Gholam236.9K
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

How about:
 
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
  Permalink  
v4
Comments
Mehdi Gholam at 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 at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

The 1st solution was not what was asked for. 2nd attempt: Wink | ;-)
 
See http://msdn.microsoft.com/en-us/library/bb397951.aspx[^] as starting point.
 
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
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 375
1 Gihan Liyanage 338
2 Vinay Mistry 160
3 Sergey Alexandrovich Kryukov 130
4 syed shanu 125
0 Sergey Alexandrovich Kryukov 9,021
1 OriginalGriff 7,941
2 CPallini 2,603
3 Richard MacCutchan 2,121
4 Abhinav S 1,928


Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 1 Feb 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100