Generating a LINQ query using Expression Trees?

Something like ( EDITED after re-reading the question) - but note that Expression. Invoke doesn't work on EF in 3.5SP1 (but it is fine in LINQ-to-SQL): using System; using System.Collections. Generic; using System.

Linq; using System.Linq. Expressions; class Dept { public string DeptName { get; set; } } public static class Program { static void Main() { IList chars = new List{'a','b'}; Dept depts = new { new Dept { DeptName = "alpha" }, new Dept { DeptName = "beta" }, new Dept { DeptName = "omega" } }; var count = testing(depts.AsQueryable(), dept => dept. DeptName, chars).Count(); } public static IQueryable testing(this IQueryable queryableData, Expression> pi, IEnumerable chars) { var arg = Expression.

Parameter(typeof(T), "x"); var prop = Expression. Invoke(pi, arg); Expression body = null; foreach(char c in chars) { Expression thisFilter = Expression. Call(prop, "StartsWith", null, Expression.

Constant(c.ToString())); body = body == null? ThisFilter : Expression. OrElse(body, thisFilter); } var lambda = Expression.

Lambda>(body? Expression. Constant(false), arg); return queryableData.

Where(lambda); } }.

Something like (EDITED after re-reading the question) - but note that Expression. Invoke doesn't work on EF in 3.5SP1 (but it is fine in LINQ-to-SQL): using System; using System.Collections. Generic; using System.

Linq; using System.Linq. Expressions; class Dept { public string DeptName { get; set; } } public static class Program { static void Main() { IList chars = new List{'a','b'}; Dept depts = new { new Dept { DeptName = "alpha" }, new Dept { DeptName = "beta" }, new Dept { DeptName = "omega" } }; var count = testing(depts.AsQueryable(), dept => dept. DeptName, chars).Count(); } public static IQueryable testing(this IQueryable queryableData, Expression> pi, IEnumerable chars) { var arg = Expression.

Parameter(typeof(T), "x"); var prop = Expression. Invoke(pi, arg); Expression body = null; foreach(char c in chars) { Expression thisFilter = Expression. Call(prop, "StartsWith", null, Expression.

Constant(c.ToString())); body = body == null? ThisFilter : Expression. OrElse(body, thisFilter); } var lambda = Expression.

Lambda>(body? Expression. Constant(false), arg); return queryableData.

Where(lambda); } }.

Perfect, thanks so much for this. I'm working on an alpha-numeric paged list (along the lines of IPagedList) and this will be invaluable. – Chao Mar 25 '10 at 9:52.

Something like ( EDITED after re-reading the question) - but note that Expression. Invoke doesn't work on EF in 3.5SP1 (but it is fine in LINQ-to-SQL).

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions