Practical Database Programming With Visual C#.NET- P4
Số trang: 50
Loại file: pdf
Dung lượng: 700.35 KB
Lượt xem: 13
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- p4, 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- P4 4.4 LINQ to Objects 173indexes of the contents of a directory tree. A simple string search is performed in thisexample. However, more complex types of pattern matching can be performed with aregular expression. Create a new C# Console project named QueryContentsLINQ, and then open thecode window of this new project and enter the codes shown in Figure 4.28 into the codewindow of this project. Let’s take a closer look at this piece of code to see how it works. A. A string object startFolder is created and the value of this object is the default path of the Visual Studio.NET 2008, in which all files of the Visual Studio.NET 2008 are installed. You can modify this path if you installed your Visual Studio.NET 2008 at a different folder in your computer. B. An IEnumerable interface is used to define the data type of the queried files fileList. The real data type applied here is System.IO.FileInfo, which is used to replace the nominal type T. The method GetFiles() is executed to open and access the queried files with the file path as the argument of this method. C. The query criteria “Visual Studio”, which is a keyword to be searched by this query, is assigned to a string object searchTerm that will be used in the following query process. D. The LINQ query is created and initialized with four clauses, from, let, where, and select. The range variable file is selected from the opened files fileList. The method GetFileText() will be executed to read back the contents of the matched files using the let clause. Two where clauses are used here to filter the matched files with both an extension .htm and a keyword “Visual Studio” in the file name. E. The Console.WriteLine() method is executed to indicate that the following matched files contain the searched keyword “Visual Studio” in their file names. F. The LINQ query is executed to pick up all files that have a file name that contains the keyword “Visual Studio”, and all searched files are displayed by using the method Console.WriteLine(). G. The purpose of these two coding lines is to allow users to run this project in a Debugging mode. H. The body of the method GetFileText() starts from here. The point is that this method must be defined as a static method prefixed with the keyword static in front of this method since it will be called from the main() method, which is a static method, too. I. The string object fileContents is initialized with an empty string object. J. The system method Exists() is executed to find all files whose names contain the keyword “Visual Studio”. All of matched files will be opened and the contents will be read back by the method ReadAllText() and assigned to the string object fileContents. K. The read-out fileContents object is returned to the calling method. L. The body of the method GetFiles() starts from here with the path as the argument of this method. The point is that this method must be defined as a static method and the returned data type is an IEnumerable type. M. An exception will be thrown out if the desired path did not exist in the current computer. N. A new nongeneric collection List is created with a Cast to convert it to the IEnumerable type. O. The system method GetFiles() is executed to find the names of all files that are under the current path and assign them to the string object array fileNames.174 Chapter 4 Introduction to Language-Integrated Query (LINQ) QueryContentsLINQ.Program Main() using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace QueryContentsLINQ { class Program { static void Main(string[] args) { // Modify this path as necessary.A string startFolder = @c:program filesMicrosoft Visual Studio 9.0; // Take a snapshot of the file system.B IEnumerable fileList = GetFiles(startFolder);C string searchTerm = @Visual Studio; // Search the contents of each file. The queryMatchingFiles is an IEnumerable.D var queryMatchingFiles = from file in fileList where file.Extension == .htm let fileText = GetFileText(file.FullName) where fileText.Contains(searchTerm) select file.FullName; // Execute the query.E Console.WriteLine(The term {0} was found in:, searchTerm);F foreach (string file ...
Nội dung trích xuất từ tài liệu:
Practical Database Programming With Visual C#.NET- P4 4.4 LINQ to Objects 173indexes of the contents of a directory tree. A simple string search is performed in thisexample. However, more complex types of pattern matching can be performed with aregular expression. Create a new C# Console project named QueryContentsLINQ, and then open thecode window of this new project and enter the codes shown in Figure 4.28 into the codewindow of this project. Let’s take a closer look at this piece of code to see how it works. A. A string object startFolder is created and the value of this object is the default path of the Visual Studio.NET 2008, in which all files of the Visual Studio.NET 2008 are installed. You can modify this path if you installed your Visual Studio.NET 2008 at a different folder in your computer. B. An IEnumerable interface is used to define the data type of the queried files fileList. The real data type applied here is System.IO.FileInfo, which is used to replace the nominal type T. The method GetFiles() is executed to open and access the queried files with the file path as the argument of this method. C. The query criteria “Visual Studio”, which is a keyword to be searched by this query, is assigned to a string object searchTerm that will be used in the following query process. D. The LINQ query is created and initialized with four clauses, from, let, where, and select. The range variable file is selected from the opened files fileList. The method GetFileText() will be executed to read back the contents of the matched files using the let clause. Two where clauses are used here to filter the matched files with both an extension .htm and a keyword “Visual Studio” in the file name. E. The Console.WriteLine() method is executed to indicate that the following matched files contain the searched keyword “Visual Studio” in their file names. F. The LINQ query is executed to pick up all files that have a file name that contains the keyword “Visual Studio”, and all searched files are displayed by using the method Console.WriteLine(). G. The purpose of these two coding lines is to allow users to run this project in a Debugging mode. H. The body of the method GetFileText() starts from here. The point is that this method must be defined as a static method prefixed with the keyword static in front of this method since it will be called from the main() method, which is a static method, too. I. The string object fileContents is initialized with an empty string object. J. The system method Exists() is executed to find all files whose names contain the keyword “Visual Studio”. All of matched files will be opened and the contents will be read back by the method ReadAllText() and assigned to the string object fileContents. K. The read-out fileContents object is returned to the calling method. L. The body of the method GetFiles() starts from here with the path as the argument of this method. The point is that this method must be defined as a static method and the returned data type is an IEnumerable type. M. An exception will be thrown out if the desired path did not exist in the current computer. N. A new nongeneric collection List is created with a Cast to convert it to the IEnumerable type. O. The system method GetFiles() is executed to find the names of all files that are under the current path and assign them to the string object array fileNames.174 Chapter 4 Introduction to Language-Integrated Query (LINQ) QueryContentsLINQ.Program Main() using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace QueryContentsLINQ { class Program { static void Main(string[] args) { // Modify this path as necessary.A string startFolder = @c:program filesMicrosoft Visual Studio 9.0; // Take a snapshot of the file system.B IEnumerable fileList = GetFiles(startFolder);C string searchTerm = @Visual Studio; // Search the contents of each file. The queryMatchingFiles is an IEnumerable.D var queryMatchingFiles = from file in fileList where file.Extension == .htm let fileText = GetFileText(file.FullName) where fileText.Contains(searchTerm) select file.FullName; // Execute the query.E Console.WriteLine(The term {0} was found in:, searchTerm);F foreach (string file ...
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