(一):預備知識
什麼是Linq to sql
Linq to sql(或者叫DLINQ)是LINQ(.NET語言集成查詢)的一部分,全稱基於關係數據的 .NET 語言集成查詢,用於以對象形式管理關係數據,並提供了豐富的查詢功能,它和Linq to xml、Linq to objects、Linq to dataset、Linq to entities等組成了強大的LINQ。
匿名類型
var data = new {username = "zhuye",age = 26};
Console.WriteLine("username:{0} age:{1}", data.username, data.age);
集合初始化器
var persons = new List<Person> {
new Person {username = "a", age=1},
new Person {username = "b", age=2}};
Lambda表達式
var list = new [] { "aa", "bb", "ac" };
var result = Array.FindAll(list, s => (s.IndexOf("a") > -1));
foreach (var v in result)
Console.WriteLine(v);
查詢句法
查詢句法是使用標準的LINQ查詢運算符來表達查詢時一個方便的聲明式簡化寫法。該句法能在代碼裏表達查詢時增進可讀性和簡潔性,讀起來容易,也容易讓人寫對。Visual Studio 對查詢句法提供了完整的智能感應和編譯時檢查支持。編譯器在底層把查詢句法的表達式翻譯成明確的方法調用代碼,代碼通過新的擴展方法和Lambda表達式語言特性來實現。上面的查詢句法等價於下面的代碼:
var persons = new List<Person> {
new Person {username = "a", age=19},
new Person {username = "b", age=20},
new Person {username = "a", age=21},
};
var selectperson = from p in persons where p.age >= 20 select p.username.ToUpper();
(二):DataContext與實體
DataContext
DataContext類型(數據上下文)是System.Data.Linq命名空間下的重要類型,用於把查詢句法翻譯成SQL語句,以及把數據從數據庫返回給調用方和把實體的修改寫入數據庫。
(四):查詢句法
select
var 構建匿名類型1 = from c in ctx.Customers
select new
{
公司名 = c.CompanyName,
地址 = c.Address
};
var 構建匿名類型3 = from c in ctx.Customers
select new
{
ID = c.CustomerID,
聯繫信息 = new
{
職位 = c.ContactTitle,
聯繫人 = c.ContactName
}
};
where
var 多條件 = from c in ctx.Customers
where c.Country == "France" && c.Orders.Count > 5
select new
{
國家 = c.Country,
城市 = c.City,
訂單數 = c.Orders.Count
};
orderby
var 排序 = from emp in ctx.Employees
where emp.Employees.Count == 0
orderby emp.HireDate.Value.Year descending, emp.FirstName ascending
select new
{
僱用年 = emp.HireDate.Value.Year,
名 = emp.FirstName
};
分頁
var Page =
booksByCategory.Skip((pageNo - 1) * PAGE_SIZE).Take(PAGE_SIZE);
分組
描述:根據顧客的國家分組,查詢顧客數大於5的國家名和顧客數
查詢句法:
var 一般分組 = from c in ctx.Customers
group c by c.Country into g
where g.Count() > 5
orderby g.Count() descending
select new
{
國家 = g.Key,
顧客數 = g.Count()
};
描述:根據國家和城市分組,查詢顧客覆蓋的國家和城市
查詢句法:
var 匿名類型分組 = from c in ctx.Customers
group c by new { c.City, c.Country } into g
orderby g.Key.Country, g.Key.City
select new
{
國家 = g.Key.Country,
城市 = g.Key.City
};
描述:按照是否超重條件分組,分別查詢訂單數量
查詢句法:
var 按照條件分組 = from o in ctx.Orders
group o by new { 條件 = o.Freight > 100 } into g
select new
{
數量 = g.Count(),
是否超重 = g.Key.條件 ? "是" : "否"
};
distinct
var 過濾相同項 = (from c in ctx.Customers orderby c.Country select c.Country).Distinct();
union
描述:查詢城市是A打頭和城市包含A的顧客並按照顧客名字排序
查詢句法:
var 連接並且過濾相同項 = (from c in ctx.Customers where c.City.Contains("A") select c).Union
(from c in ctx.Customers where c.ContactName.StartsWith("A") select c).OrderBy(c => c.ContactName);
concat
描述:查詢城市是A打頭和城市包含A的顧客並按照顧客名字排序,相同的顧客信息不會過濾
查詢句法
var 連接並且不過濾相同項 = (from c in ctx.Customers where c.City.Contains("A") select c).Concat
(from c in ctx.Customers where c.ContactName.StartsWith("A") select c).OrderBy(c => c.ContactName);
取相交項
描述:查詢城市是A打頭的顧客和城市包含A的顧客的交集,並按照顧客名字排序
查詢句法:
var 取相交項 = (from c in ctx.Customers where c.City.Contains("A") select c).Intersect
(from c in ctx.Customers where c.ContactName.StartsWith("A") select c).OrderBy(c => c.ContactName);
排除相交項
描述:查詢城市包含A的顧客並從中刪除城市以A開頭的顧客,並按照顧客名字排序
查詢句法:
var 排除相交項 = (from c in ctx.Customers where c.City.Contains("A") select c).Except
(from c in ctx.Customers where c.ContactName.StartsWith("A") select c).OrderBy(c => c.ContactName);
子查詢
描述:查詢訂單數超過5的顧客信息
查詢句法:
var 子查詢 = from c in ctx.Customers
where
(from o in ctx.Orders group o by o.CustomerID into o where o.Count() > 5 select o.Key).Contains(c.CustomerID)
select c;
in操作
描述:查詢指定城市中的客戶
查詢句法:
var in操作 = from c in ctx.Customers
where new string[] { "Brandenburg", "Cowes", "Stavern" }.Contains(c.City)
select c;
join
描述:內連接,沒有分類的產品查詢不到
查詢句法:
var innerjoin = from p in ctx.Products
join c in ctx.Categories
on p.CategoryID equals c.CategoryID
select p.ProductName;