LINQ to SQL Table浅谈

 

LINQ有很多值得学习的地方,这里我们主要介绍LINQ to SQL Table,包括介绍LINQ的核心概念等方面。

近日开始写有关于LINQ的文章,正巧写到Linq To SQL,由于探索LINQ的核心概念所致,脑中突现一个想法,"我是否可以将LINQ to SQL Table与LINQ to XML的XElement join起来?"

理论上,在LINQ的设计概念中,这是可行的.

 1 static void TestCrossLinq()
 2 {
 3 NORTHWND db = new NORTHWND("Data Source=.\\SQLEXPRESS;
 4 Initial Catalog=NORTHWND;Integrated Security=True");
 5 XDocument doc = XDocument.Load("XMLFile1.xml");
 6 var p = from s1 in doc.Elements("tables").Elements("table").
 7 Descendants("row")
 8 join s2 in db.Customers on s1.Element("CUSTOMER_ID").
 9 Value equals s2.CustomerID
10 where s1.Parent.Attribute("name") != null &&
11 s1.Parent.Attribute("name").Value == "Orders"
12 select new XElement("Order", s1.Nodes(), 
13 new XElement("CompanyName",s2.CompanyName));
14 foreach (var item in p)
15 {
16 foreach (var item3 in item.Elements())
17 {
18 Console.WriteLine("{0} : {1}", item3.Name, item3.Value);
19 Console.WriteLine("--------------------");
20 }
21 }
22 Console.ReadLine();
23 }

 

此程式由XML中读出Order资讯,以其CUSTOMER_ID Element中的资料来与Linq To SQL Table : Customers join,取出CompanyName栏位放入结果集.

 

 

 
posted @ 2012-05-31 19:14  Peter.Luo  阅读(269)  评论(0)    收藏  举报