O'Reilly logo

WCF 4.0 Multi-tier Services Development with LINQ to Entities by Mike Liu

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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>)); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required