LINQ的一些技巧

2021-09-08 21:01:57 字數 843 閱讀 4241

1.陣列初始化

大小為10的陣列,每個元素值都是-1
int a = enumerable.repeat(-1, 10).toarray();

大小為10的陣列,從0至9遞增

int b = enumerable.range(0, 10).toarray();

大小為10的陣列,從100,110,120,...,190

int c = enumerable.range(0, 10).select(i => 100 + 10 * i).toarray();

2.生成隨機數序列

生成10個範圍在10-100的隨機數

random rand = new random();

int randomseq= randomseq = enumerable.repeat(0, 10).select(i => rand.next(10,100)).toarray();

3.集合型別轉換

int集合轉成string集合

list intlist = new list ;

list strlist = new list(intlist.cast());

反過來,把string集合轉成int集合

list a = strlist.select(o => int.parse(o)).tolist();

4.陣列倒序

int arr = ;

arr.reverse();

現在arr的元素已經是5,4,3,2,1了

LINQ 的 一些語句

1.找出b1在a1中 以逗號分隔的 完全匹配的字串結果 stringa1 abc,efg,hik,lmn,opq stringb1 efg,d3l,opq,lmn var result a1.split intersect b1.split intersect 通過使用預設的相等比較取出兩個序列的交...

LINQ的一些學習

class introtolinq 2.query creation.numquery is an ienumerablevar numquery from num in numbers where num 2 0 select num 3.query execution.foreach int n...

LINQ 的一些相關基本語法

型別 查詢變數 from 臨時變數in 集合物件或資料庫物件 where 條件表示式 order by 條件 select 臨時變數中被查詢的值 group by 條件 簡單的查詢 var movies from c in db.movies select c 此處是乙個集合 sql select ...