LINQ动态查询运用的人很少,也许因为排斥,也许因为难以实现,本文笔者就为大家介绍几种LINQ动态查询方法。
在LINQ动态查询中,Lambda表达式是许多标准查询运算符的基础,编译器创建lambda表达式以捕获基础查询方法(例如 Where、Select、Order By、Take While 以及其他方法)中定义的计算。表达式目录树用于针对数据源的结构化查询,这些数据源实现IQueryable
例如,LINQ to SQL 提供程序实现 IQueryable
表达式目录树在LINQ中用于表示分配给类型为Expression
System.Linq.Expressions命名空间提供用于手动生成表达式目录树的API。Expression类包含创建特定类型的表达式目录树节点的静态工厂方法,例如,ParameterExpression(表示一个已命名的参数表达式)或 MethodCallExpression(表示一个方法调用)。编译器生成的表达式目录树的根始终在类型Expression
下面几个例子描述如何使用表达式目录树来创建LINQ动态查询。
1.LINQ动态查询之Select下面例子说明如何使用表达式树依据 IQueryable 数据源构造一个动态查询,查询出每个顾客的ContactName,并用GetCommand方法获取其生成SQL语句。
- //依据IQueryable数据源构造一个查询
- IQueryable
custs = db.Customers; - //组建一个表达式树来创建一个参数
- ParameterExpression param = Expression.Parameter(typeof(Customer), "c");
- //组建表达式树:
- c.ContactNameExpression selector = Expression.Property(param,
- typeof(Customer).GetProperty("ContactName"));
- Expression pred = Expression.Lambda(selector, param);
- //组建表达式树:
- Select(c=>c.ContactName)Expression expr =
- Expression.Call(typeof(Queryable), "Select",
- new Type[] { typeof(Customer), typeof(string) },
- Expression.Constant(custs), pred);
- //使用表达式树来生成动态查询
- IQueryable<string> query =
- db.Customers.AsQueryable() .Provider.CreateQuery<string>(expr);
- //使用GetCommand方法获取SQL语句
- System.Data.Common.DbCommand cmd =
- db.GetCommand(query);Console.WriteLine(cmd.CommandText);
生成的SQL语句为:
- SELECT [t0].[ContactName] FROM [dbo].[Customers] AS [t0]
2.LINQ动态查询之Where下面一个例子是“搭建”Where用法来动态查询城市在伦敦的顾客。
- IQueryable
custs = db.Customers; - //创建一个参数
- cParameterExpression param =
- Expression.Parameter(typeof(Customer), "c");
- c.City=="London"Expression left = Expression.Property(param,
- typeof(Customer).GetProperty("City"));
- Expression right = Expression.Constant("London");
- Expression filter = Expression.Equal(left, right);
- Expression pred = Expression.Lambda(filter, param);
- Where(c=>c.City=="London")Expression expr =
- Expression.Call(typeof(Queryable),
- "Where", new Type[] { typeof(Customer) },
- Expression.Constant(custs), pred);
- //生成动态查询IQueryable
query = - db.Customers.AsQueryable() .Provider.CreateQuery
(expr);
生成的SQL语句为:
- SELECT [t0].[CustomerID], [t0].[CompanyName], [t0].[ContactName],
- [t0].[ContactTitle], [t0].[Address], [t0].[City], [t0].[Region],
- [t0].[PostalCode], [t0].[Country], [t0].[Phone],
- [t0].[Fax]FROM [dbo].[Customers] AS [t0] WHERE [t0].[City] =
- @p0-- @p0: Input NVarChar (Size = 6; Prec = 0; Scale = 0) [London]
3.LINQ动态查询之OrderBy本例既实现排序功能又实现了过滤功能。
- IQueryable
custs = db.Customers; - //创建一个参数cParameterExpression param =
- Expression.Parameter(typeof(Customer), "c");
- c.City=="London"Expression left = Expression.Property(param,
- typeof(Customer).GetProperty("City"));Expression right =
- Expression.Constant("London");
- Expression filter = Expression.Equal(left, right);Expression pred =
- Expression.Lambda(filter, param);
- Where(c=>c.City=="London")MethodCallExpression whereCallExpression =
- Expression.Call( typeof(Queryable), "Where",
- new Type[] { typeof(Customer) }, Expression.Constant(custs), pred);
- OrderBy(ContactName =>
- ContactName)MethodCallExpression orderByCallExpression =
- Expression.Call( typeof(Queryable), "OrderBy",
- new Type[] { typeof(Customer), typeof(string) },
- whereCallExpression,
- Expression.Lambda(Expression.Property (param, "ContactName"), param));
- //生成动态查询
- IQueryable
query = - db.Customers.AsQueryable().Provider.CreateQuery
- (orderByCallExpression);
生成的SQL语句为:
- SELECT [t0].[CustomerID], [t0].[CompanyName], [t0].[ContactName],
- [t0].[ContactTitle], [t0].[Address], [t0].[City],
- [t0].[Region],[t0].[PostalCode],
- [t0].[Country], [t0].[Phone],
- [t0].[Fax]FROM [dbo].[Customers] AS [t0] WHERE [t0].[City] =
- @p0ORDER BY [t0].[ContactName]-- @p0:
- Input NVarChar (Size = 6; Prec = 0; Scale = 0) [London]
4.LINQ动态查询之Union下面的例子使用表达式树动态查询顾客和雇员同在的城市。
- //e.CityIQueryable
custs = db.Customers; - ParameterExpression param1 = Expression.Parameter(typeof(Customer), "e");
- Expression left1 = Expression.Property(param1,
- typeof(Customer).GetProperty("City"));
- Expression pred1 = Expression.Lambda(left1, param1);
- c.CityIQueryable
employees = - db.Employees;ParameterExpression param2 =
- Expression.Parameter(typeof(Employee), "c");
- Expression left2 = Expression.Property(param2,
- typeof(Employee).GetProperty("City"));
- Expression pred2 = Expression.Lambda(left2, param2);
- Select(e=>e.City)Expression expr1 =
- Expression.Call(typeof(Queryable), "Select",
- new Type[] { typeof(Customer), typeof(string) },
- Expression.Constant(custs), pred1);
- Select(c=>c.City)Expression expr2 =
- Expression.Call(typeof(Queryable), "Select",
- new Type[] { typeof(Employee), typeof(string) },
- Expression.Constant(employees), pred2);
- //生成动态查询
- IQueryable<string> q1 =
- db.Customers.AsQueryable().Provider.CreateQuery<string>(expr1);
- IQueryable<string> q2 =
- db.Employees.AsQueryable().Provider.CreateQuery<string>(expr2);
- //并集
- var q3 = q1.Union(q2);
生成的SQL语句为:
- SELECT [t2].[City]
- FROM
- ( SELECT [t0].[City] FROM [dbo].[Customers] AS [t0]
- UNION SELECT [t1].[City] FROM [dbo].[Employees] AS [t1] )
- AS [t2]
以上就是关于LINQ动态查询的一些方法。
【编辑推荐】