#眉標=VS2008、、 #副標=體驗新一代整合開發環境(19) #大標=多用途查詢語言LINQ資料操作實務 #作者=文/圖 王寧疆 ===<反灰>============= string startFolder = @"C:\Program Files\Microsoft Visual Studio 9.0"; //指定欲搜尋的資料夾 IEnumerable fileList = GetFiles(startFolder); //呼叫GetFiles方法,取得資料夾下所有的檔案名稱 System.Text.RegularExpressions.Regex searchTerm = new System.Text.RegularExpressions.Regex( @"Visual (Basic|C#|C\+\+|SourceSafe|Studio)"); //建立Regex類別的物件,指定欲搜尋的內容 var queryMatchingFiles =from file in fileList where file.Extension == ".htm" let fileText = System.IO.File.ReadAllText(file.FullName) let matches = searchTerm.Matches(fileText) where searchTerm.Matches(fileText).Count > 0 select new { name = file.FullName, matches = from System.Text.RegularExpressions.Match match in matches select match.Value }; //搜尋副檔名為.HTM的檔案內容是否有出現指定的字串 foreach (var v in queryMatchingFiles) //取得所有內容包含指定字串內容的檔案名稱 { Control.WriteLine(v.name); //顯示檔案名稱 } ================ ===<反灰>============= IEnumerable GetFiles(string path) { if (!System.IO.Directory.Exists(path)) //如果指定的資料夾不存在 throw new System.IO.DirectoryNotFoundException(); //引發例外 string[] fileNames = null; //存放檔案名稱的陣列 List files = new List(); //建立存放FileInfo類別的物件的集合 fileNames = System.IO.Directory.GetFiles(path, "*.*", SearchOption.AllDirectories); //取出資料夾(包含子資料夾在內)底下所有的檔案名稱 foreach (string name in fileNames) //取出所有的檔案名稱 { files.Add(new System.IO.FileInfo(name)); //依據檔案名稱建立FileInfo類別的名稱, //再加入到名稱為files的集合中 } return files; //傳回名稱為files的集合 } ================ ===<反灰>============= public class dbOrders { public IQueryable BindOrders(int startRowIndex, int maximumRows)//具分頁功能的查詢方法 { NorthwindDataContext dc = new NorthwindDataContext(); //建立DataContext類別的物件 var query = from order in dc.Orders select order; //取出Orders資料表的記錄 return query.Skip(startRowIndex).Take(maximumRows); //從startRowIndex編號起,取出maximumRows筆記錄 } public int GetOrderCount() //查詢Orders的資料筆數 { NorthwindDataContext dc = new NorthwindDataContext(); //建立DataContext類別的物件 return (from order in dc.Orders select order).Count(); //傳回Orders的資料筆數 } } ================ ==<反灰>=========== exec sp_executesql N'SELECT [t1].[OrderID], [t1].[CustomerID], [t1].[EmployeeID], [t1].[OrderDate], [t1].[RequiredDate], [t1].[ShippedDate], [t1].[ShipVia], [t1].[Freight], [t1].[ShipName], [t1].[ShipAddress], [t1].[ShipCity], [t1].[ShipRegion], [t1].[ShipPostalCode], [t1].[ShipCountry] FROM ( SELECT ROW_NUMBER() OVER (ORDER BY [t0].[OrderID], [t0].[CustomerID], [t0].[EmployeeID], [t0].[OrderDate], [t0].[RequiredDate], [t0].[ShippedDate], [t0].[ShipVia], [t0].[Freight], [t0].[ShipName], [t0].[ShipAddress], [t0].[ShipCity], [t0].[ShipRegion], [t0].[ShipPostalCode], [t0].[ShipCountry]) AS [ROW_NUMBER], [t0].[OrderID], [t0].[CustomerID], [t0].[EmployeeID], [t0].[OrderDate], [t0].[RequiredDate], [t0].[ShippedDate], [t0].[ShipVia], [t0].[Freight], [t0].[ShipName], [t0].[ShipAddress], [t0].[ShipCity], [t0].[ShipRegion], [t0].[ShipPostalCode], [t0].[ShipCountry] FROM [dbo].[Orders] AS [t0] ) AS [t1] WHERE [t1].[ROW_NUMBER] BETWEEN @p0 + 1 AND @p0 + @p1 ORDER BY [t1].[ROW_NUMBER]',N'@p0 int,@p1 int',@p0=30,@p1=10 ============= ==<反灰>=========== class Products { public int ProductID { get; set; } public int SupplierID { get; set; } public string CompanyName { get; set; } } ============= ==<反灰>=========== private IEnumerable GetProducts(int ProductID) { //LINQ對northwindDataSet中的Products和Suppliers進行JOIN操作,並讀取Products的ProductID欄位與Suppliers的SupplierID和CompanyName欄位當做結果 var DataSet = from p in northwindDataSet.Products join s in northwindDataSet.Suppliers on p.SupplierID equals s.SupplierID where p.ProductID == ProductID select new Products { ProductID = p.ProductID, SupplierID = p.SupplierID, CompanyName = s.CompanyName }; return DataSet.ToList(); //傳回JOIN運算結果 } ============= ==<反灰>=========== dataGridView1.DataSource = GetProducts(產品編號); ============= ==<反灰>=========== from o in Orders where o.CustomerID=="ALFKI" select o =============