快速业务通道

LINQ体验(17)——LINQ to SQL语句之动态查询

作者 佚名技术 来源 NET编程 浏览 发布时间 2012-05-21
don")
Expression expr = Expression.Call(typeof(Queryable), "Where",
  new Type[] { typeof(Customer) },
  Expression.Constant(custs), pred);
//生成动态查询
IQueryable<Customer> query = db.Customers.AsQueryable()
   .Provider.CreateQuery<Customer>(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.OrderBy

本例既实现排序功能又实现了过滤功 能。

IQueryable<Customer> custs = db.Customers;
//创建一个参数c
ParameterExpression 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<Customer> query = db.Customers.AsQueryable()
  .Provider.CreateQuery<Customer> (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] = @p0
ORDER BY [t0].[ContactName]
-- @p0: Input NVarChar (Size = 6; Prec = 0; Scale = 0) [London]

4.Union

下面的例子使用表达式树动态查询顾客和雇员同在的 城市。

//e.City
IQueryable<Customer> 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.City
IQueryable<Employee> 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), type

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号