LINQ統一資料操作語句

2021-07-05 17:24:32 字數 4811 閱讀 7084

1.資料來源無關的資料統一操作,linq語句做了乙個程式語句到各種資料來源之間的抽象統一中介軟體。where開始,select或group結束. order 和group,select new等各種語句

。2.用了拓展方法,在generic列舉型別來做,編譯器會將linq語句解釋為where上的列舉型別的事例物件上呼叫拓展方法

,實現功能。因為拓展方法的拓展介面是ienumerable所以所有繼承該介面的實現類都支援該拓展方法。

不是所有的資料篩選都可以用預定義的拓展方法linq來表示,所以有時候要自己定義linq函式和拓展方法;拓展方法工作方式是直接from r in語句活得資料來源結構物件集合呼叫where方法,返回結果集再呼叫select或者group方法;linq運算順序都是從左到右的,如果巢狀的from語句那麼外部和內部將有兩個傳入引數作為selectmany的輸入,提供乙個內部的型別作為約束,外部的型別輸出和繼續使用後面的約束。

3. 用了列舉器來做,linq只是定義遇到yield return標記返回不執行,foreach才執行yield return執行下去,轉換為其它容器或tolookup時候會立即執行

。但是注意中間資料結構元素的變化會迭代出不同的結果,如果最後用了tolist等就不會等到迭代時才返回集合。

linq上的操作,where選定資料集,資料查詢篩選,邏輯表示式和比較表示式支援,開始偏移和擷取長度搜尋個數,前面開始後面開始搜尋,排序,分組,資料連線外部內部,集合操作,聚合函式,檢視集合轉換產生,判斷是否滿足條件的函式。

1).篩選from 巢狀from

2).排序orderby語句,以及和 thenby多個組合語句。

3).分組group,group r by r.x into 別名 。是用groupby拓展方法來進行分組。

select new可以從分組的結果裡面再建立新的統計結果。

4).連線jiont操作

5).集合操作交並差,可以將查詢語句封裝為方法或limban表示式.

6).zip(),take(),skip()方法。

7).聚合函式

8).轉換為其它容器, 查詢會在迭代時候執行,而轉換為容器時候卻會立即執行。

tolookup函式和非型別化的集合上使用linq就可以用cast()方法作為轉換。

9)range,empty,repeat()不是拓展方法,而是集合類的正常靜態方法,也會推遲執行,基於原來型別構建迭代器。

具體多查詢資料和實踐程式設計。

在.net 4.0後增加的,通過system.linq中的新類,parallelenumerable可以分解查詢工作到多個執行緒中,parallelenumerable大多數拓展方法是parallelquery的拓展,乙個例外是asparallel()方法它拓展了ienumerable介面,返回parallelquery類,所以正常集合可以並行方式查詢。

應用:1)正常查詢用容器上containername.asparallel()方法,那麼所有的where,select,sum都會變成parallel版本的。

2)partitioner.create(containername,true).asparallel()來並行查詢。

3)取消,呼叫withcancellation方法,且傳遞乙個cancellationtoken作為令牌引數建立,需要呼叫時候用令牌cancel()就可以了,例如:

例如:

static void main()

static void cancellation()

var cts = new cancellationtokensource();

new thread(() =>

", sum);

}catch (operationcanceledexception ex)

}).start();

console.writeline("query started");

console.write("cancel? ");

int input = console.read();

if (input == 'y' || input == 'y')

}static void introparallel()

stopwatch watch = new stopwatch();

watch.start();

var q1 = (from x in partitioner.create(data).asparallel()

where x < 80

select x).sum();

watch.stop();

console.writeline(watch.elapsedmilliseconds);

}}

表示式樹是乙個可以改變語義分析的編譯器級別的功能,也就是可以為編寫的不同的表示式語句,定義不同的執行函式。

通過樹形結構來呼叫不同的處理函式。

例如:lambda表示式可以用於委託中,也可以用於expression型別中作為引數,expression有很多表示式的型別派生類,可以分析執行表示式,例如:

class program

! nodetype: ; expr: ",

"".padleft(indent, '>'), message, expression.nodetype, expression);

indent++;

switch (expression.nodetype)

displaytree(indent, "body", lambdaexpr.body);

break;

case expressiontype.constant:

constantexpression constexpr = (constantexpression)expression;

console.writeline(" const value: ", output, constexpr.value);

break;

case expressiontype.parameter:

parameterexpression paramexpr = (parameterexpression)expression;

console.writeline(" param type: ", output, paramexpr.type.name);

break;

case expressiontype.equal:

case expressiontype.andalso:

case expressiontype.greaterthan:

binaryexpression binexpr = (binaryexpression)expression;

if (binexpr.method != null)

method: ", output, binexpr.method.name);

}else

displaytree(indent, "left", binexpr.left);

displaytree(indent, "right", binexpr.right);

break;

case expressiontype.memberaccess:

memberexpression memberexpr = (memberexpression)expression;

console.writeline(" member name: , type: ", output,

memberexpr.member.name, memberexpr.type.name);

displaytree(indent, "member expr", memberexpr.expression);

break;

default:

console.writeline();

console.writeline(" ", expression.nodetype, expression.type.name);

break;}}

static void main()

}

上述是lambda表示式用於expression型別。

linq to sql中lambda表示式用於expression型別。

也可以定義自己的expression拓展方法型別和linq表示式,實現自己的表示式樹,這樣表示式樹存在於程式集中,執行的時候會讀取。

linq語句.net框架中定義的目標物件有:linq to objects, linq to xml,linq to sql。

其中linq語句具體的解釋由作用的資料來源容器型別來決定。

例如linq to objects中的whered拓展方法定義為:

public static ienumerablewhere(this ienumerables source, funcpredicate);
linq to sql中的where拓展方法定義為:

public static iqueryablewhere(this iqueryablesource, expression> predicate);

當在sql中的where語句時候,因為作用的容器資料來源型別是ado中定義的objectcontext類的createquery()方法返回乙個實現了iqueryable介面的objectquery物件,因此時候使用queryable類的where()方法。

linq 是比較深奧的主題,主要是用到了表示式樹的函式式語言的程式設計思想,拓展方法拓展當前的資料來源自定義拓展方法,資料來源上進行資料的操作會建立各種型別的容器和需要的委託方法(lambda表示式),以及對各種資料來源底層細節的封裝對上層提供了統一的linq簡易資料操作。

統一資料交換 UDX

udx unified data exchange system統一資料交換系統 udx是乙個分布式的資料物件交換系統。儲存在不同平台,資料庫,應用系統的資料通過udx能夠以資料物件為單位進行交換。乙個資料物件是乙個有意義的業務實體。udx系統的組成如下圖所示 交換主體是需要交換資料的系統,如乙個資...

C 資料操作LINQ

linq是language integrated query的縮寫,即 語言整合查詢 之意。linq主要包含四個元件,linq to object linq to xml linq to dataset和linq to sql。本文僅涉及前兩個。示例 using system using syste...

資料結構(一) 陣列的操作

最近開始學習資料結構,覺得這門課蠻重要的,不論是刷題還是寫安卓,資料結構還是要學好的,學習資料結構,我還是用c語言來實現,好理解一點。資料結構中最基本的乙個結構就是線性結構,而線性結構又分為連續儲存結構和離散儲存結構。所謂的連續儲存結構其實就是陣列。陣列本質其實也是資料的一種儲存方式,既然有了資料的...