C 學習筆記之 LINQ

2021-09-24 12:57:39 字數 1201 閱讀 6284

linq(language integrated query)

linq是.net框架的擴充套件,它允許我們以使用sql查詢資料庫的方式來查詢資料集合。

使用linq,你可以從資料庫,程式物件的集合以及xml文件中查詢資料。

查詢語句

using system;

using system.linq;

namespace test

; var numsquery = from n in numbers

where n < 20

select n;//查詢語句

var numsmethod = numbers.where(x => x < 20);//方法語句

int numscount = (from n in numbers

where n < 20

select n).count();//兩種結合

foreach (var x in numsquery)

", x);

}console.writeline();

foreach (var x in numsmethod)

", x);

}console.writeline();

console.writeline(numscount);}}

}

from 迭代變數 in 要查詢的集合名字

join 指定另外的集合和id引用它 on 第乙個集合的項 equals 第二個集合的項

from...let...where:

static void main()

; var groupb = new ;

var someints = from a in groupa

from b in groupb

let sum = a + b

where sum == 12

select new ;

foreach (var a in someints)

console.writeline(a);

}

from...orderby...select

static void main()

;

var result = from

}

待補充。。

Linq 學習筆記之 linq to object

take 方法 取出集合中前幾個元素 eg var listtop list.take 3 takewhile 方法 takewhile 方法用於取序列中從頭開始算起符合條件的元素直到遇到不符合條件的元素為止。eg string names var takenames names.takewhile...

linq 學習筆記之 Linq基本子句

1 from 子句 乙個linq表示式,必須是以from子句開頭。var value from v in values where v.indexof it 1 select v 2復合from子句 多重巢狀查詢 便於自己理解 var query from a in aa from b in a.b...

LINQ 學習筆記之儲存過程!

首先在資料庫中隨便建立兩個儲存過程,乙個待引數,乙個不待引數,create proc sp selectstudent asbegin select from student end第二個 create proc dbo sp addclasses classename nvarchar 20 as...