LINQ to SQL Table淺談
LINQ有很多值得學(xué)習(xí)的地方,這里我們主要介紹LINQ to SQL Table,包括介紹LINQ的核心概念等方面。
近日開始寫有關(guān)于LINQ的文章,正巧寫到Linq To SQL,由于探索LINQ的核心概念所致,腦中突現(xiàn)一個想法,"我是否可以將LINQ to SQL Table與LINQ to XML的XElement join起來?"
理論上,在LINQ的設(shè)計概念中,這是可行的.
- static void TestCrossLinq()
- {
- NORTHWND db = new NORTHWND("Data Source=.\\SQLEXPRESS;
Initial Catalog=NORTHWND;Integrated Security=True");- XDocument doc = XDocument.Load("XMLFile1.xml");
- var p = from s1 in doc.Elements("tables").Elements("table").
Descendants("row")- join s2 in db.Customers on s1.Element("CUSTOMER_ID").
Value equals s2.CustomerID- where s1.Parent.Attribute("name") != null &&
- s1.Parent.Attribute("name").Value == "Orders"
- select new XElement("Order", s1.Nodes(),
new XElement("CompanyName",s2.CompanyName));- foreach (var item in p)
- {
- foreach (var item3 in item.Elements())
- {
- Console.WriteLine("{0} : {1}", item3.Name, item3.Value);
- Console.WriteLine("--------------------");
- }
- }
- Console.ReadLine();
- }
此程式由XML中讀出Order資訊,以其CUSTOMER_ID Element中的資料來與Linq To SQL Table : Customers join,取出CompanyName欄位放入結(jié)果集.
【編輯推薦】