LINQ to SQL语句(23)之动态查询
系列文章导航:
LINQ to SQL语句(2)之Select/Distinct
LINQ to SQL语句(3)之Count/Sum/Min/Max/Avg
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语句(12)之Delete和使用Attach
LINQ to SQL语句(14)之Null语义和DateTime
LINQ to SQL语句(19)之ADO.NET与LINQ to SQL
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页]