Linq学习笔记(2.1)——初识 DLinq
1:创建实体类
首先为你项表达的数据库数据建立实体类,以后的Demo所用的数据库为Northwind;
针对产品表(Products)创建产品类
[Table(Name = "Products")]
public class Product
{
[Column(IsPrimaryKey = true)]
public int ProductID;
[Column]
public string ProductName;
[Column]
public int CategoryID;
[Column]
public decimal UnitPrice;
[Column]
public bool Discontinued;
} 为Product类附加Table特性,表示该类对应数据库的Products表,为Product类的属性附加Column特性对应Products表中的列,Column特性有很多属性,如Name,CanBeNull方便你精确表达数据库表的特征,和上例中[Column(IsPrimaryKey=true)]表示用Product的ProductID属性表达数据库表Products的主键。
2:DataContext为你从数据库中取回对象或提交修改的管道,
DataContext db = new DataContext("data source=.\\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=Northwind;");
Table<Product> Products = db.GetTable<Product>();
var Discontinueds = from p in Products
where p.Discontinued == true
select p;
var DiscontinuedsCount = Discontinueds.Count();
Response.Write(string.Format("一共{0}个产品被废弃,他们是:<br>", DiscontinuedsCount));
foreach (var p in Discontinueds)
Response.Write(string.Format("<div class='showinfo'>price: {1}; name: {0}</div>", p.ProductName, p.UnitPrice.ToString()));推荐定义一个强类型的DataContext取代基本的DataContext类型,然后声明所有的Table集合为Context的成员。
public partial class Northwind : DataContext
{
public Northwind(string connection) : base(connection) { }
public Table<Product> Products;
//public Table<Order> Orders;
}
Northwind db = new Northwind("data source=.\\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=Northwind;");
Table<Product> Products = db.Products;
var Discontinueds = from p in Products
where p.UnitPrice > 50
orderby p.UnitPrice
select p;
foreach (var p in Discontinueds)
Response.Write(string.Format("<div class='showinfo'>price: {1}; name: {0}</div>", p.ProductName, p.UnitPrice.ToString()));3:定义关系:
在关系型数据库中通过主外键来表达各个表之间的关系,Dlinq定义了一个Association特性,你将它附加在你的实体类的某个成员上来表达各个实体间的关系。
using
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Northwind db = new Northwind("data source=.\\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=Northwind;");
var product = (from p in db.Products
where p.ProductID == 50
select p)
.First();//var product=db.Products.Single(p=>p.ProductID==50);
var orderCount=product.OrderDetails.Count;
Response.Write(string.Format("产品{0}被订购{1}次,订单ID分别为:<br>", product.ProductName, orderCount));
foreach (var o in product.OrderDetails)
Response.Write(string.Format("<span class='showinfo'>{0}</span>", o.OrderID));
}
}
定义产品实体
定义订单细节实体
构建强类型的DataContext——Northwind除了上面直接将关系定义在实体内部外,还有一种简便方法,直接用Linq交叉查询的方法在两个Table集合中联合查询。
var order = from p in db.Products
from o in db.OrderDetails
where p.ProductID == 50 && o.ProductID==p.ProductID
select new { p.ProductName,o.OrderID};
Response.Write(string.Format("产品{0}被订购{1}次,订单ID分别为:<br>", order.First().ProductName, order.Count()));
foreach (var o in order)
Response.Write(string.Format("<span class='showinfo'>{0}</span>", o.OrderID)); 
4:添加,删除,修改实体
对实体的修改,删除等操作也一样非常简单,你完全可以忘记你现在是在操作数据库!
var product1 = db.Products.Single(p => p.ProductID == 1);
product1.ProductName = "Young's book";
Product product2 = new Product { ProductName = "Young's Computer" };
db.Products.Add(product2);
db.Products.Remove(product1);
db.SubmitChanges();

浙公网安备 33010602011771号