Practical Database Programming With Visual C#.NET- P5
Số trang: 50
Loại file: pdf
Dung lượng: 1.46 MB
Lượt xem: 14
Lượt tải: 0
Xem trước 5 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
Tham khảo tài liệu practical database programming with visual c#.net- p5, công nghệ thông tin, cơ sở dữ liệu phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả
Nội dung trích xuất từ tài liệu:
Practical Database Programming With Visual C#.NET- P5 4.9 C# 3.0 Language Enhancement for LINQ 223 foreach (var fi in facultyUpdates.Elements(facultyUpdate)) { Faculty faculty = db.Faculties. First(f => f.faculty_id == (string)fi.Element(faculty_id)); faculty.Phone = (string)fi.Element(phone); } db.SubmitChanges();Figure 4.67 Piece of sample code to read and update database.tion of compositional APIs that have equal expressive power of query languages indomains such as relational databases and XML. Compared with C# 2.0, significant enhancements have been added into C# 3.0, andthese enhancements are mainly developed to support the Language-Integrated Query.LINQ is a series of language extensions that supports data querying in a type-safe way; itis released with the latest version Visual Studio, Visual Studio.NET 2008. The data to bequeried, which we have discussed in the previous sections in this chapter, can take theform of objects (LINQ to Objects), databases (LINQ-enabled ADO.NET, which includesLINQ to SQL, LINQ to DataSet, and LINQ to Entities), XML (LINQ to XML), andso on. In addition to those general LINQ topics, special improvements on LINQ aremade for C# and involved in C# 3.0. The main components of these improvementsinclude: • Lambda expressions • Extension methods • Implicitly typed local variables • Query expressions Let’s have a detailed discussion of these topics one by one.4.9.1 Lambda ExpressionsLambda expressions are a language feature that is similar in many ways to anonymousmethods. In fact, if lambda expressions had been developed and implemented into thelanguage first, there would have been no need for anonymous methods. The basic ideaof using lambda expressions is that you can treat code as data. In the early version C#,such as C# 1.0, it is very common to pass strings, integers, reference types, and so on tomethods so that the methods can work on those values. Anonymous methods and lambdaexpressions extend the range of the values to include code blocks. This concept is commonin functional programming. The syntax of lambda expressions can be expressed as a comma-delimited list ofparameters with the lambda operator (=>) followed by an expression. For more compli-cated lambda expressions, a statement block can be followed after the lambda operator.A simple example of lambda expression used in C# looks like: x => y224 Chapter 4 Introduction to Language-Integrated Query (LINQ)where x on the left side of the lambda operator is the input parameter and the y on theright side of the lambda operator is the output parameter. The data type of both the inputand the output parameters should be explicitly indicated by the delegate. Therefore thelambda expressions are closely related to delegate. This lambda expression can be readas input x and output y. The syntax of this kind of simple lambda expressions can bewritten as: (param1, param2, …. paramN) => output A parenthesis should be used to cover all input parameters. For more complicated lambda expressions, a statement block should be adopted. Anexample of this kind of syntax is shown below: (x, y) => { if (x > y) return x; else return y; } Note that the data type of both the input and the output parameters must be identicalwith those types defined in the delegate. For example, in the previous sample expressionx => y, if the input x is defined as a string, and the output is defined as an integer by thedelegate, the output must be converted to an integer type by using the following lambdaexpression: x => y.Lengthwhere Length is a method to convert the input from a string to an integer. Another example of using lambda expressions to perform LINQ query is: IEnumerable faculty = EnumerableExtensions.Where(faculties, f => f.faculty_name == “Ying Bai”); Here the SQO method Where() is used as a filter in this query. The input is an objectwith a type of faculties, and the output is a string variable. The compiler is able to infer that“f ” refers to a faculty because the first parameter of the Where() method isIEnumerable, such that T must, in fact, be Faculty. Using this knowledge,the compiler also verifies that Faculty has a faculty_name member. Finally, there is noreturn key word specified. In the syntactic form, the return member is omitted but this ismerely syntactic convenience. The result of the expression is still considered to be thereturn value. Lambda expressions also support a more verbose syntax that allows you to specifythe types explicitly, as well as execute multiple statements. An example of this kind ofsyntax is: return EnumerableExtensions.Where(faculties, (Faculty f) => {string id =faculty_id; return f.faculty_id = ...
Nội dung trích xuất từ tài liệu:
Practical Database Programming With Visual C#.NET- P5 4.9 C# 3.0 Language Enhancement for LINQ 223 foreach (var fi in facultyUpdates.Elements(facultyUpdate)) { Faculty faculty = db.Faculties. First(f => f.faculty_id == (string)fi.Element(faculty_id)); faculty.Phone = (string)fi.Element(phone); } db.SubmitChanges();Figure 4.67 Piece of sample code to read and update database.tion of compositional APIs that have equal expressive power of query languages indomains such as relational databases and XML. Compared with C# 2.0, significant enhancements have been added into C# 3.0, andthese enhancements are mainly developed to support the Language-Integrated Query.LINQ is a series of language extensions that supports data querying in a type-safe way; itis released with the latest version Visual Studio, Visual Studio.NET 2008. The data to bequeried, which we have discussed in the previous sections in this chapter, can take theform of objects (LINQ to Objects), databases (LINQ-enabled ADO.NET, which includesLINQ to SQL, LINQ to DataSet, and LINQ to Entities), XML (LINQ to XML), andso on. In addition to those general LINQ topics, special improvements on LINQ aremade for C# and involved in C# 3.0. The main components of these improvementsinclude: • Lambda expressions • Extension methods • Implicitly typed local variables • Query expressions Let’s have a detailed discussion of these topics one by one.4.9.1 Lambda ExpressionsLambda expressions are a language feature that is similar in many ways to anonymousmethods. In fact, if lambda expressions had been developed and implemented into thelanguage first, there would have been no need for anonymous methods. The basic ideaof using lambda expressions is that you can treat code as data. In the early version C#,such as C# 1.0, it is very common to pass strings, integers, reference types, and so on tomethods so that the methods can work on those values. Anonymous methods and lambdaexpressions extend the range of the values to include code blocks. This concept is commonin functional programming. The syntax of lambda expressions can be expressed as a comma-delimited list ofparameters with the lambda operator (=>) followed by an expression. For more compli-cated lambda expressions, a statement block can be followed after the lambda operator.A simple example of lambda expression used in C# looks like: x => y224 Chapter 4 Introduction to Language-Integrated Query (LINQ)where x on the left side of the lambda operator is the input parameter and the y on theright side of the lambda operator is the output parameter. The data type of both the inputand the output parameters should be explicitly indicated by the delegate. Therefore thelambda expressions are closely related to delegate. This lambda expression can be readas input x and output y. The syntax of this kind of simple lambda expressions can bewritten as: (param1, param2, …. paramN) => output A parenthesis should be used to cover all input parameters. For more complicated lambda expressions, a statement block should be adopted. Anexample of this kind of syntax is shown below: (x, y) => { if (x > y) return x; else return y; } Note that the data type of both the input and the output parameters must be identicalwith those types defined in the delegate. For example, in the previous sample expressionx => y, if the input x is defined as a string, and the output is defined as an integer by thedelegate, the output must be converted to an integer type by using the following lambdaexpression: x => y.Lengthwhere Length is a method to convert the input from a string to an integer. Another example of using lambda expressions to perform LINQ query is: IEnumerable faculty = EnumerableExtensions.Where(faculties, f => f.faculty_name == “Ying Bai”); Here the SQO method Where() is used as a filter in this query. The input is an objectwith a type of faculties, and the output is a string variable. The compiler is able to infer that“f ” refers to a faculty because the first parameter of the Where() method isIEnumerable, such that T must, in fact, be Faculty. Using this knowledge,the compiler also verifies that Faculty has a faculty_name member. Finally, there is noreturn key word specified. In the syntactic form, the return member is omitted but this ismerely syntactic convenience. The result of the expression is still considered to be thereturn value. Lambda expressions also support a more verbose syntax that allows you to specifythe types explicitly, as well as execute multiple statements. An example of this kind ofsyntax is: return EnumerableExtensions.Where(faculties, (Faculty f) => {string id =faculty_id; return f.faculty_id = ...
Tìm kiếm theo từ khóa liên quan:
thủ thuật máy tính công nghệ thông tin tin học quản trị mạng computer networkTài liệu liên quan:
-
52 trang 433 1 0
-
24 trang 359 1 0
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 320 0 0 -
Làm việc với Read Only Domain Controllers
20 trang 310 0 0 -
74 trang 303 0 0
-
96 trang 297 0 0
-
Báo cáo thực tập thực tế: Nghiên cứu và xây dựng website bằng Wordpress
24 trang 291 0 0 -
Đồ án tốt nghiệp: Xây dựng ứng dụng di động android quản lý khách hàng cắt tóc
81 trang 285 0 0 -
EBay - Internet và câu chuyện thần kỳ: Phần 1
143 trang 277 0 0 -
Tài liệu hướng dẫn sử dụng thư điện tử tài nguyên và môi trường
72 trang 270 0 0