Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Please check below code
C#
List<Emp> empD = new List<Emp> { new Emp { Name = "a", age = 1 }, new Emp { Name = "b", age = 2 } };
            var test = "empD.Where(x=>x.age ==1 )";
            var param1 = Expression.Parameter(typeof(Emp), "test");
            var lambda = System.Linq.Dynamic.DynamicExpression.ParseLambda(new[] { param1 }, typeof(Emp), test);
            var rest = lambda.Compile();

Here i am trying to evaluate lambda expression but input will be given in string param , which stores expression.How can i evaluate to get list of entity.

Thanks in Advance
Jayanth
Posted
Updated 14-Dec-14 22:55pm
v3
Comments
Tomas Takac 15-Dec-14 4:57am    
Shouldn't the name of the parameter param1 be x?
Member 8118083 15-Dec-14 5:48am    
trying to get typeof Emp from the string value "test"; as i tried with giving "x" or "empD" it gives me an exception.

Thinking to solve this in different approach (may be by changing tree to iqueryable).But not sure what extent its correct.

Thanks
Maciej Los 15-Dec-14 5:47am    
Why?
BillWoodruff 15-Dec-14 9:37am    
Thanks for the heads-up on my response here: I really missed the boat on this one :)
Maciej Los 15-Dec-14 10:12am    
;) You're very welcome, Bill ;)

1 solution

Please, read my comment to the question. I asked there: why do you want to evaluate lambda expression. You didn't answered. Nevertheless...

In a few words: lambda expression is nothing else than inline delegate!

Have a look here:
Func<T1, T2, TResult> Delegate[^]
Exploring Lambda Expression in C#[^]

Conclusion: you can use it even as a parameter of function.
See:
Dynamic Queries and LINQ Expressions[^]
http://stackoverflow.com/questions/14297633/c-sharp-pass-lambda-expression-as-method-parameter[^]

Example (works like a charm):
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LinqDelegate
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Emp> empD = new List<Emp> { new Emp("a", 1),
                                    new Emp("b", 2),
                                    new Emp("c", 1),
                                    new Emp("d", 4) };

            Func<Emp, int, bool> predicate = (e, i) => e.Age==1;

            var qry = empD.Where(predicate).Select(x => x);
            foreach (var e in qry)
                Console.WriteLine("{0} = {1}", e.Name, e.Age);

            Console.ReadKey();

        }

        class Emp
        {
            private string sName = String.Empty;
            private int iAge = 0;

            public Emp(string _Name, int _Age)
            {
                sName = _Name;
                iAge = _Age;
            }

            public string Name
            {
                get { return sName; }
                set { sName = value; }
            }

            public int Age
            {
                get { return iAge; }
                set { iAge = value; }
            }
        }
    }
}

Result:
a = 1
c = 1
 
Share this answer
 
v4
Comments
BillWoodruff 15-Dec-14 13:23pm    
+5
Maciej Los 15-Dec-14 14:22pm    
Thank you, Bill ;)
BillWoodruff 16-Dec-14 5:40am    
On re-reading the OP, and your response, I am left with the question: what is the best way ... given a lambda expression in a string ... to transform that into an executable Linq query ?
Maciej Los 16-Dec-14 6:20am    
Best? I don't know, Bill. I'm not so familiar with it. I think that parsing(evaluating) expression from string consumes more RAM. So, it decreases a performance.
BillWoodruff 16-Dec-14 8:20am    
Hi Maciej, I don't know, either :) but, I have kept the list of sources I compiled for the reply I removed here for future evaluation.

There are strategies for inserting string elements in to a linq query so they are late-bound dynamically evaluated ... that's clear.

cheers, Bill

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