前面我已经写了3编LINQ的文章,所谓的3编,真正来说只是一编而已,第2、第3都是写C#的新特性了,有些朋友觉得上我的当了,呵呵,在此向大家道个谦。
LINQ学习之旅(1)
LINQ学习之旅(2)C#新特性 扩展方法
LINQ学习之旅(3)C#新特性 自动属性
现在继续学习LINQ。
LINQ与数据库
![]()
}
在控制台会输出一段很长的SQL词句,有兴趣自己看看吧,呵呵。
LINQ TO XML
var xml = from p in db.Products.Skip(3).Take(4)
select new XElement("Product",
new XAttribute("ID", p.ProductID),
new XElement("ProductName", p.ProductName),
new XElement("CategoryName", p.Categories.CategoryName)
);

new XDocument(new XDeclaration("1.0", "utf-8", "yes"),
new XElement("Products", xml)).Save("C:/Products.xml");//把查询到的数据以XML文档输出到C:/Products.xml
new XDocument(new XDeclaration("1.0", "utf-8", "yes"),
new XElement("Products", xml)).Save(Console.Out);//把查询到的数据以XML文档形式输出到控制台
Console.WriteLine(new XDocument(new XDeclaration("1.0", "utf-8", "yes"),
new XElement("Products", xml)).ToString()); //这方法也是查询到的数据以XML文档形式输出到控制台
有了LINQ TO XML,那以后的AJAX的实现就更方便了。
var table = from p in db.Products.Skip(3).Take(4)
select new XElement("tr",
new XElement("td", p.ProductID),
new XElement("td", p.ProductName),
new XElement("td", p.Categories.CategoryName)
);
Console.WriteLine(new XDocument(new XElement("table", table)).ToString());
相关文章: