LINQ補充:
強制立即執行:
執行聚合函數(Count,Max,Average,First)
調用ToList(TSource>)或ToArrat(TSource>)方法緩存結果
使用let擴展范圍變量:(數據源為一個字符串數組)
var earlyBirdQuery =
from sentence in strings
let words = sentence.Split(' ')
from word in words
let w = word.ToLower()
where w[0]=='a||w[0]=='e'||
w[0]==t
select word;
let words = sentence.Split(' ') 創建一個可以查詢自身的可枚舉類型
let w = word.ToLower() 使查詢只能對范圍變量word調用一次ToLower.如果
不使用let,則必須在where子句的每個布爾條件中調用ToLwer()
篩選:
where cust.City=="London"&&cust.Name=="Devon"
where cust.City=="London"||cust.City=="paris"
使用C#邏輯AND和OR運算符來根據需要在Where子句中應用任意數量的篩選表達
式.
除了不能是第一個或最后一個子句外,它幾乎可以放在查詢表達式中的任何位置.
排序:
在查詢表達式中,orderby子句可以使返回的序列或子序列按升序或降序排序
可以指定多個鍵,以便執行一個或多個次要排序操作
默認排序順序為升序
編譯時,orderby子句被轉換為OrderBy方法的調用.orderby子句中的多個鍵轉換為
ThenBy方法調用.
分組:
group子句返回一個IGrouping(TKey,Telement)對象序列
編譯時,group子句被轉換為對GroupBy方法的調用.
var studentQuery1= from student in students
group student by student.Last[0];
如果想要對每個組執行附加查詢操作,則可以使用into上下文關鍵字執行一個臨時
標示符.使用into時,必須繼續編寫該查詢,并最終使用一個select語句或另外一個
group子句結束該查詢
var studentQuery2 = from student in students
group student by student.Last[0] into g
orderby g.Key
select g;
聯接:
使用join子句可以將 來自不同源序列并且在對象模型中沒有直接關系的元素相關聯
唯一的要求是每個源中的元素需要共享某個可以進行比較判斷是否相等的值
join子句使用特殊的equals關鍵字比較指定的鍵是否相等(只支持equals進行判斷)
三種常見的聯接類型
內部聯接
分組聯接
左外聯接
內聯:
var innerJoinQuery = from category in categores
join prod in products on category.ID equals
prod.CategoryID select new
{Productname==prod.Name,Category=category.name};
分組:含有into 表達式的join子句稱為分組聯接
var innerJoinQuery = from category in categores
join prod in products on category.ID equals
prod.CategoryID into prodGroup select new
{CategoryName=category.Name, products=prodGroup};
左外:
在左外聯接中,將返回左側源序列中的所有元素,即使它們在右側序列中沒有匹配的元
素也是如此.
若要在LINQ中執行左外聯接,請將DefaultIfEmpty方法與分組聯接結合起來,以指定
要在某個左側元素不具有匹配元素時產生的默認右側元素.可以使用 null作為任何引
用類型的默認值,也可以指定用戶定義的默認類型
var leftOuterJoinQuery= from category in categories
join prod in products on category.ID equals
prod.CategoryID into prodGroup
from item in prodGroup.DefaultIfEmpty(new
Product{Name=String.Empty,CategoryID=0}) select new
{CateName=category.Name,ProdName=item.Name};
join選擇(投影):
select 子句可以指定將在執行查詢時產生的值的類型.該子句的結果將基于前面所有
子句的計算結果以及select子句本身中的所有表達式.
查詢表達式必須以select子句group子句結束
在最簡單的情況下,select子句僅指定范圍變量.這會使返回的序列包含與數據源具有
相同類型的元素.
強制立即執行:
執行聚合函數(Count,Max,Average,First)
調用ToList(TSource>)或ToArrat(TSource>)方法緩存結果
使用let擴展范圍變量:(數據源為一個字符串數組)
var earlyBirdQuery =
from sentence in strings
let words = sentence.Split(' ')
from word in words
let w = word.ToLower()
where w[0]=='a||w[0]=='e'||
w[0]==t
select word;
let words = sentence.Split(' ') 創建一個可以查詢自身的可枚舉類型
let w = word.ToLower() 使查詢只能對范圍變量word調用一次ToLower.如果
不使用let,則必須在where子句的每個布爾條件中調用ToLwer()
篩選:
where cust.City=="London"&&cust.Name=="Devon"
where cust.City=="London"||cust.City=="paris"
使用C#邏輯AND和OR運算符來根據需要在Where子句中應用任意數量的篩選表達
式.
除了不能是第一個或最后一個子句外,它幾乎可以放在查詢表達式中的任何位置.
排序:
在查詢表達式中,orderby子句可以使返回的序列或子序列按升序或降序排序
可以指定多個鍵,以便執行一個或多個次要排序操作
默認排序順序為升序
編譯時,orderby子句被轉換為OrderBy方法的調用.orderby子句中的多個鍵轉換為
ThenBy方法調用.
分組:
group子句返回一個IGrouping(TKey,Telement)對象序列
編譯時,group子句被轉換為對GroupBy方法的調用.
var studentQuery1= from student in students
group student by student.Last[0];
如果想要對每個組執行附加查詢操作,則可以使用into上下文關鍵字執行一個臨時
標示符.使用into時,必須繼續編寫該查詢,并最終使用一個select語句或另外一個
group子句結束該查詢
var studentQuery2 = from student in students
group student by student.Last[0] into g
orderby g.Key
select g;
聯接:
使用join子句可以將 來自不同源序列并且在對象模型中沒有直接關系的元素相關聯
唯一的要求是每個源中的元素需要共享某個可以進行比較判斷是否相等的值
join子句使用特殊的equals關鍵字比較指定的鍵是否相等(只支持equals進行判斷)
三種常見的聯接類型
內部聯接
分組聯接
左外聯接
內聯:
var innerJoinQuery = from category in categores
join prod in products on category.ID equals
prod.CategoryID select new
{Productname==prod.Name,Category=category.name};
分組:含有into 表達式的join子句稱為分組聯接
var innerJoinQuery = from category in categores
join prod in products on category.ID equals
prod.CategoryID into prodGroup select new
{CategoryName=category.Name, products=prodGroup};
左外:
在左外聯接中,將返回左側源序列中的所有元素,即使它們在右側序列中沒有匹配的元
素也是如此.
若要在LINQ中執行左外聯接,請將DefaultIfEmpty方法與分組聯接結合起來,以指定
要在某個左側元素不具有匹配元素時產生的默認右側元素.可以使用 null作為任何引
用類型的默認值,也可以指定用戶定義的默認類型
var leftOuterJoinQuery= from category in categories
join prod in products on category.ID equals
prod.CategoryID into prodGroup
from item in prodGroup.DefaultIfEmpty(new
Product{Name=String.Empty,CategoryID=0}) select new
{CateName=category.Name,ProdName=item.Name};
join選擇(投影):
select 子句可以指定將在執行查詢時產生的值的類型.該子句的結果將基于前面所有
子句的計算結果以及select子句本身中的所有表達式.
查詢表達式必須以select子句group子句結束
在最簡單的情況下,select子句僅指定范圍變量.這會使返回的序列包含與數據源具有
相同類型的元素.
浙公網安備 33010602011771號