TAILIEUCHUNG - ASP.NET 4 Unleased - p 102

Bạn có thể xây dựng các biểu thức LINQ để truy vấn SQL động bằng cách tận dụng các lớp . Lớp này có tất cả các phương pháp để xây dựng các biểu thức truy vấn năng động. Đây là danh sách (rất một phần) các phương pháp hỗ trợ của lớp này. | 984 CHAPTER 20 Data Access with LINQ to SQL You can build LINQ to SQL query expressions dynamically by taking advantage of the class. This class contains all the methods for building query expressions dynamically. Here is a very partial list of methods supported by this class Add Creates an expression that represents addition. And Creates an expression that represents a logical AND. Condition Creates an expression that represents a condition. Constant Creates an expression that represents a constant value. Convert Creates an expression that represents a conversion from one type to another. Divide Creates an expression that represents division. Equal Creates an expression that represents whether two expressions are equal. Field Creates an expression that represents a field. Lambda Creates a lambda expression. Multiply Creates an expression that represents multiplication. Or Creates an expression that represents a logical OR. Parameter Creates an expression that represents a function parameter. Property Creates an expression that represents accessing a property. PropertyOrField Creates an expression that represents accessing a property or field. Subtract Creates an expression that represents subtraction. Again this is not a complete list of methods supported by the Expression class. However it should give you some idea of how you can go about building expressions. Let s discuss a real-world situation in which you need dynamic LINQ to SQL expressions sorting. If you want to enable sorting when using a GridView control with LINQ to SQL you have a choice. You can create a switch SELECT CASE block to sort by every possible column that a user can click or you can create a dynamic LINQ to SQL expression. The class in Listing contains a method called GetDynamicSort that returns a dynamic lambda expression that can be used with either the OrderBy or OrderByDescending method. LISTING Standard App Code using System using