您好,登錄后才能下訂單哦!
本篇內容介紹了“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動態查詢的方法有哪些”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。