您的位置:知识库 »

LINQ to SQL语句(23)之动态查询

作者: 李永京  来源: 博客园  发布时间: 2008-09-18 18:34  阅读: 19232 次  推荐: 2   原文链接   [收藏]  
摘要:这个系列的第二十三篇,讲解动态查询。
[1] Select与Where
[2] OrderBy与Union

系列文章导航:

LINQ to SQL语句(1)之Where

LINQ to SQL语句(2)之Select/Distinct

LINQ to SQL语句(3)之Count/Sum/Min/Max/Avg

LINQ to SQL语句(4)之Join

LINQ to SQL语句(5)之Order By

LINQ to SQL语句(6)之Group By/Having

LINQ to SQL语句(7)之Exists/In/Any/All/Contains

LINQ to SQL语句(8)之Concat/Union/Intersect/Except

LINQ to SQL语句(9)之Top/Bottom和Paging和SqlMethods

LINQ to SQL语句(10)之Insert

LINQ to SQL语句(11)之Update

LINQ to SQL语句(12)之Delete和使用Attach

LINQ to SQL语句(13)之开放式并发控制和事务

LINQ to SQL语句(14)之Null语义和DateTime

LINQ to SQL语句(15)之String

LINQ to SQL语句(16)之对象标识

LINQ to SQL语句(17)之对象加载

LINQ to SQL语句(18)之运算符转换

LINQ to SQL语句(19)之ADO.NET与LINQ to SQL

LINQ to SQL语句(20)之存储过程

LINQ to SQL语句(21)之用户定义函数

LINQ to SQL语句(22)之DataContext

LINQ to SQL语句(23)之动态查询

LINQ to SQL语句(24)之视图

LINQ to SQL语句(25)之继承

LINQ简介

adsfsaf


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), 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]
[第1页][第2页]
2
4
标签:LINQ LINQ to SQL

热门文章

    最新文章

      最新新闻

        热门新闻