Dynamic query
In addition to using LINQ syntax we can also build queries dynamically. There are two ways to build a query dynamically—using expressions and using parameters. In this section we will explain both of these two methods.
Dynamic query with expressions
First let's build a dynamic query with expressions. The following code will create two method expressions: one for the where
clause and one for the order
by
clause:
static void TestDynamicQuery() { NorthwindEntities NWEntities = new NorthwindEntities(); ParameterExpression param = Expression.Parameter(typeof(Product), '"p'"); Expression left = Expression.Property(param, typeof(Product).GetProperty('"UnitPrice'")); Expression right = Expression.Constant((decimal)100.00, typeof(System.Nullable<decimal>)); Expression filter = Expression.GreaterThanOrEqual(left, right); Expression pred = Expression.Lambda(filter, param); IQueryable products = NWEntities.Products; Expression expr = Expression.Call(typeof(Queryable), '"Where...