讀書筆記 第十一章 C in depth

2021-08-29 20:46:32 字數 4128 閱讀 5487

思考

//11-2

from user in sampledata.allusers

select user;

轉譯=>sampledata.allusers.select(user => user);

範圍變數都可以是隱式型別。cast,oftype將飛型別化序列轉化為強型別。 遇到不匹配型別,cast報錯,oftype跳過。

//11-5

arraylist list = new arraylist ;

ienumerablestrings = list.cast();

list = new arraylist ;

ienumerableints = list.oftype();

//11-8

user tim = sampledata.users.testertim;

var query = from defect in sample

where defect.status != status.closed

where defect.assignedto = tim

select defect.summary;

.select(defect => defect)
查詢表示式的結果和源資料永遠不會是同乙個物件,對返回資料集的改變也不會影響到「主」資料。

7.16.2.4 from, let, where, join and orderby clauses

a query expression with a let clausefrom x in e let y = fis translated intofrom * in ( e ) . select ( x => new )

inner = left, outer = right 內連線從某個物件導航到另乙個物件。對右邊序列進行緩衝,對左邊序列進行流處理。 在sql中,內連線通常是把某個表的外來鍵和另乙個表的主鍵進行連線。

//11-12

from defect in sampledata.alldefects

join subscription in sampledata.allsubscriptions

on defect.project equals subcription.project

select new ;

send defect to subcripter's emailbox

=>轉譯=>

leftsequence.join(rightsequence,

leftkeyselector,

rightkeyselector,

resultselector)

from defect in sampledata.alldefects

where defect.status == status.closed

join subscription in sampledata.allsubscriptions

on defect.project equals subcription.project

select new ;

from subscription in sampledata.allsubscriptions

join defect in (from defect in sampledata.alldefects

where defect.status == status.closed

select defect)

on defect.project equals subcription.project

select new ;

//11-13

from defect in sampledata.alldefects

join subscription in sampledata.allsubscriptions

on defect.project equals subcription.project

into groupedsubscriptions

select new ;

group join != group by: 對於分組連線來說,左邊序列和結果序列是一對一,左邊元素不匹配任何右邊元素時,嵌入序列是空的。

=>轉譯=>.groupjoin()

不存在序列間的匹配操作,結果包含了所有可能的元素對。笛卡爾積(cartesian join)

//11-15

from user in sampledata.allusers

from project in sampledata.allprojects

select new

// 11-16

from left in enumerable.range(1, 4)

from right in enumerable.range(11, left)

select new ;

=>轉譯=>.selectmany()

鍵和序列的組合封裝於igrouping: ienemerable中。

// 11-17

from defect in sampledata.alldefects

where defect.assignedto != null

group defect by defect.assignedto;

=>轉譯=>.groupby()

group by 的投影

//11-18

from defect in sampledata.alldefects

where defect.assignedto != null

group defect.summary by defect.assignedto;

q. find scope of defect, grouped and result

// 11-20 

from defect in sampledata.alldefects

where defect.assignedto != null

group defect by defect.assignedto into grouped

select new into result

orderby result.count descending

select result;

=>轉譯=>

sampledata.alldefects

.where(defect => defect.assignedto != null)

.groupby(defect => defect.assignedto)

.select(gouped => new )

.orderbydescending(result => result.count);

轉譯成extension method:查詢表示式在編譯之前,先被轉譯為普通的c# (查詢操作符、點標記:enumerable中的擴充套件方法).

查詢表示式

點標記-

沒有相應查詢表示式: .reverse(), .todictionary() 等

-特定過載

-自定義比較器

-清晰可讀

多個lambda表示式,多個呼叫:join 的鍵選擇

-排序的多個優先順序:orderby f1, f2

.orderby(f1).thenby(f2)

// 在匿名型別中直接使用lambda表示式引數

sequence.select((item, index) => new );

c sharp in depth

linq to objects (c#) msdn

published in my cs blog: 

第十一章 讀書筆記

第十一章 linux驅動程式中的 併發控制 併發 concurrency 指的是多個執行單元同時 並行被執行。而併發的執行單元對共享資 源 如硬體資摞 程式中的全域性變數 靜態變數等 的訪問很容易導致競態條件 race conditions 自旋鎖並不關心鎖定的 臨界區究竟是怎樣的操作,不管是讀還是...

第十一章讀書筆記

併發 concurrency 指的是多個執行單元同時 並行被執行。而併發的執行單元對共享資 源 如硬體資摞 程式中的全域性變數 靜態變數等 的訪問很容易導致競態條件 race conditions 例如,有乙個裝置檔案。程序 a 向該個裝置檔案寫入 1000 個 a飛而程序 b 向 裝置檔案寫入了 ...

APUE讀書筆記 第十一章 執行緒

新建立的執行緒可以訪問程序的位址空間,並且繼承呼叫執行緒的浮點環境和訊號遮蔽字,但是該執行緒的掛起訊號集會被清除 新執行緒時呼叫pthread self函式獲取自己的執行緒id,而不是從共享記憶體中讀出,或者從執行緒的啟動歷程中以引數的形式接收到的,因為新執行緒可能在主線程呼叫pthread cre...