前面我已经写了3编LINQ的文章,所谓的3编,真正来说只是一编而已,第2、第3都是写C#的新特性了,有些朋友觉得上我的当了,呵呵,在此向大家道个谦。
LINQ学习之旅(1)
LINQ学习之旅(2)C#新特性 扩展方法
LINQ学习之旅(3)C#新特性 自动属性
现在继续学习LINQ。
LINQ与数据库
/**/
////
// 这是LINQ的与数据库部分的,很像强类型DataSet
/**/ ////
NorthwindDataContext db = new NorthwindDataContext();
db.Log = Console.Out; // 把SQL脚本输出到控制台
var products =
from p in db.Products.Skip( 6 ).Take( 4 )
select new
{
p.ProductName,
p.ProductID,
p.Categories.CategoryName,
} ;
// Skip()和Take()这两个方法的结合,就可以类似于储存过程分页一样
// Skip(int startIndex)从第 startIndex + 1 开始 因为它是下标
// Take(int count)一共取count条数据
// select new {p.ProductName, p.ProductID, p.Categories.CategoryName,}
// 就像我们写SQL的 select 词句一样只要ProductName,ProductID,CategoryName这三列
// 返回的proudcts就是只以上三个属性的对象的集合
foreach (var p in products)
{
Console.WriteLine(p.ProductID + ":" + p.ProductName + ":" + p.CategoryName);
}
// 这是LINQ的与数据库部分的,很像强类型DataSet
/**/ ////
NorthwindDataContext db = new NorthwindDataContext();
db.Log = Console.Out; // 把SQL脚本输出到控制台
var products =
from p in db.Products.Skip( 6 ).Take( 4 )
select new
{
p.ProductName,
p.ProductID,
p.Categories.CategoryName,
} ;
// Skip()和Take()这两个方法的结合,就可以类似于储存过程分页一样
// Skip(int startIndex)从第 startIndex + 1 开始 因为它是下标
// Take(int count)一共取count条数据
// select new {p.ProductName, p.ProductID, p.Categories.CategoryName,}
// 就像我们写SQL的 select 词句一样只要ProductName,ProductID,CategoryName这三列
// 返回的proudcts就是只以上三个属性的对象的集合
foreach (var p in products)
{
Console.WriteLine(p.ProductID + ":" + p.ProductName + ":" + p.CategoryName);
}
在控制台会输出一段很长的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文档形式输出到控制台
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());
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());