Programming Visual Basic 2008 phần 7

Để thực hiện sự hiểu biết của chúng ta về đầu ra cho mỗi truy vấn dễ dàng hơn, chúng ta hãy giả vờ rằng tôi đã viết một phương pháp hiển thị các kết quả của bất kỳ truy vấn dưới hình thức bảng. Tôi sẽ gọi ShowResults thường xuyên. | New Book With _ .Title Ben-Hur _ .AuthorlD LW .Pages 544 New Book With _ .Title Peter Pan _ .AuthorID IB .Pages 192 To make our understanding of the output for each query easier let s pretend that I ve written a method that displays the results of any query in table form. I ll call the routine ShowResults. Basic Query Expressions LINQ expressions are built from query clauses that have the same feel as clauses in SQL statements at the database level. With the exception of the From clause which must appear first the other clauses can generally appear in any order within the query. The From Clause Every basic LINQ query begins with the From keyword. Dim bookBag From bk In Library ShowResults bookBag Results -- War and Peace LT 1424 Anna Karenina LT 976 Ben-Hur LW 544 Peter Pan IB 192 This four-word query is pretty much the shortest LINQ query you can write. I stored the results of the query in the bookBag variable with its data type inferred by the query but the query can also be used directly as an expression. ShowResults From bk In Library The bk variable included in the query is known as a range variable or iteration variable. You don t have to use bk I just chose that name at random. It s a variable so give it any name you wish. This variable provides a way to identify objects and object members from the source data within the query. Since Library is a collection it wouldn t make sense to say when referring to the title of just one book. Instead you refer to . Personally I find this variable In source syntax a little indirect. I much prefer the table-alias syntax used in SQL queries. SELECT FROM Library AS bk The SQL AS keyword performs much the same function as the LINQ In keyword. Yet despite my internal tension the In syntax prevails you cannot use the AS syntax in LINQ since the As keyword in Visual Basic is used for data type assignment. Basic Query Expressions 453 The Select Clause If you use only the From clause

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU MỚI ĐĂNG
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.