linq查询表达式的基本语法很容易掌握,它使用c#常见的语言构造,从外观上看,和我们常用的sql类似,并且查询表达式中的变量可以用匿名类型,所以在很多情况下,不需要指定变量类型就可以构建linq表达式。
linq的数据源可以是数据库对象或是xml流等,也可以使实现了ienumerable或者泛型ienumberable<t>接口的集合对象。
linq的基本语法包含如下的8个上下文关键字,这些关键字和具体的说明如下:
关键字 |
|
from | 指定范围变量和数据源 |
where | 根据bool表达式从数据源中筛选数据 |
select | 指定查询结果中的元素所具有的类型或表现形式 |
group | 对查询结果按照键值进行分组(igrouping<tkey,telement>) |
into | 提供一个标识符,它可以充当对join、group或select子句结果的引用 |
orderby | 对查询出的元素进行排序(ascending/descending) |
join | 按照两个指定匹配条件来equals连接两个数据源 |
let | 产生一个用于存储查询表达式中的子表达式查询结果的范围变量 |
下面依此总结这8个关键字的常用查询语句写法。
1.from子句
如果要写一个linq表达式,就必须是以from子句开头。个人觉得from子句中需要注意的地方就是多个from子句的书写。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.collections;
namespace linq
{
/// <summary>
/// linq,重点是感悟from子句中的查询变量的灵活
/// </summary>
class program
{
static void main( string [] args)
{
//1单个form子句
string [] values = { "linq学习" , "linq基本语句" , "from子句" , "单个from子句" };
var value = from v in values
where v.indexof( "linq" ) > -1
select new { v, v.length };
foreach (var n in value)
{
console.writeline( "{0},{1}" ,n.v,n.length );
}
console.readkey( false );
//2使用linq查询arraylist
arraylist glist = new arraylist();
glist.add( new gustinfo { name= "debuglzq" , age=26, tel= "88888888" });
glist.add( new gustinfo { name= "博客园" ,age=6, tel = "666666" });
glist.add( new gustinfo { name = "m&msoft" , age =9, tel = "55555" });
var query = from gustinfo gust in glist
where gust.age > 9
select gust; //范围变量gust制定了数据类型
foreach (gustinfo g in query)
{
console.writeline( "{0} 年龄:{1} 电话:{2}" ,g.name,g.age,g.tel );
}
console.readkey( false );
//3复合from子句
list<gustinfo2> glist2 = new list<gustinfo2>()
{
new gustinfo2{ name= "debuglzq" ,age=26,teltable= new list< string >(){ "8888888" , "138******" }},
new gustinfo2{ name= "博客园" ,age=6,teltable = new list< string >(){ "666666" , "138******" }},
new gustinfo2{ name= "m&msoft" ,age=9,teltable= new list< string >(){ "55555" , "138******" }}
};
//gust、tel都是查询变量,作用域为当前查询语句!!!
var query2 = from gust in glist2
from tel in gust.teltable
where tel.indexof( "5555" ) > -1
select gust;
foreach (var g in query2)
{
console.writeline( "{0} 年龄{1}" ,g.name,g.age );
foreach (var t in g.teltable)
{
console.writeline( "电话:{0}" ,t);
}
}
console.readkey( false );
//4多个from子句
var query3 = from gustinfo gust in glist
where gust.age > 6
from gustinfo2 gust2 in glist2
where gust2.age> 9
select new { gust, gust2 }; //查询结果定制
foreach (var g in query3)
{
console.writeline( "{0} {1}" , g.gust.name, g.gust2.name);
}
console.readkey( false );
}
}
}
|
程序的运行结果如下:
程序中列举了from子句的用法示例,注意复合from子句和多个from子句的书写,同时需要理解范围变量和数据源变量这两个概念。
2.where子句
where子句,它是linq表达式的元素筛选机制,除了开始和结束的位置,它几乎可以出现在linq表达式的任意位置上。
在一个linq表达式中,可以有where子句,也可以没有;可以有一个,也可以有多个;多个where子句之间的逻辑关系相当于逻辑“与”,每个where子句可以包含1个或多个bool逻辑表达式,这些条件成为谓词,谓词逻辑之间用的是“&&”“||”等而不是sql中的and 、or。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
using system;
using system.collections.generic;
using system.linq;
using system.text;
namespace linq_where
{
/// <summary>
/// linq where子句
/// </summary>
class program
{
static void main( string [] args)
{
//1常见的where语句
list<gustinfo> glist = new list<gustinfo>()
{
new gustinfo(){ name= "debuglzq" , age=26,tel= "88888888" },
new gustinfo(){ name= "cnblogs" ,age=6,tel= "666666" },
new gustinfo(){ name= "m&msoft" ,age=9,tel= "55555" }
};
var query = from gust in glist
where (gust.name.length > 7 || gust.name.substring(0, 1) == "m" ) && gust.age > 9
select new { gust.name, gust.age };
foreach (var g in query)
{
console.writeline( "{0},{1}" , g.name, g.age);
}
console.readkey( false );
//2.在where子句中使用自定义函数
var query2 = from gustinfo gust in glist
where gust.name.length > 5
&& check(gust.name)
select gust;
foreach (var g in query2)
{
console.writeline( "{0},{1},{2}" , g.name, g.age, g.tel);
}
console.readkey( false );
//3.动态谓词的筛选
//定义动态谓词数组,在实际开发中可以动态获得
string [] names = { "sb" , "xxx" , "***" , "@@@" , "一些敏感词" };
var query3 = from gustinfo guest in glist
where !names.contains(guest.name)
select guest;
foreach (var q in query3)
{
console.writeline( "{0} 年龄:{1},电话:{2}" ,q.name,q.age,q.tel );
}
console.readkey( false );
}
//自定义函数
static bool check( string name)
{
if (name.substring(0, 1) == "n" )
return false ;
return true ;
}
}
}
|
需要注意一些常用的where子句的写法。程序的运行结果如下:
3.select子句
在select子句上可以非常灵活的处理查询到的元素,然后再把结果返回。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
using system;
using system.collections.generic;
using system.linq;
using system.text;
namespace linq_select
{
/// <summary>
/// linq select
/// 在select子句上,可以非常灵活的处理查询到的元素,然后再把结果返回
/// </summary>
class mygustinfo
{
public string name { get ; set ; }
public int age { get ; set ; }
}
class program
{
static void main( string [] args)
{
list<gustinfo> glist = new list<gustinfo>()
{
new gustinfo(){ name= "debuglzq" , age=25, tel= "88888888" },
new gustinfo(){ name= "cnblogs" , age=6, tel= "666666" },
new gustinfo(){ name= "m&m" , age=9, tel= "55555" }
};
var query = from gust in glist
where gust.age >= 9 && gust.age <= 30
select gust.name.replace( "&" , "mm" ); //select子句灵活应用
var query2 = from gust in glist
where gust.age >= 9 && gust.age <= 30
select myproc(gust.name);
var query3 = from gust in glist
where gust.age >= 9 && gust.age <= 30
select new { gust.name,gust.age};
var query4 = from gust in glist
where gust.age >= 9 && gust.age <= 30
select new mygustinfo { name=gust.name+ "my" , age=gust.age+1}; //对查询结果进行投影
foreach (var v in query)
{
console.writeline(v);
}
foreach (var v in query2)
{
console.writeline(v);
}
foreach (var v in query3)
{
console.writeline(v.name+v.age );
}
foreach (var v in query4)
{
console.writeline(v.name+v.age );
}
console.readkey( false );
}
static string myproc( string s)
{
return s + "better" ;
}
}
}
|
程序的运行结果如下:
4.group子句
根据语法的规定,linq表达式必须以from子句开头,以select或group子句结束,所以除了使用select来返回结果外,也可以使用group子句来返回元素分组后的结果。
group子句返回的是一个基于igrouping<tkey,telement>泛型接口的对象序列。
语法和sql的group有点区别,不注意的话可能会写错。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
using system;
using system.collections.generic;
using system.linq;
using system.text;
namespace linq_group
{
/// <summary>
/// linq group子句
/// </summary>
class program
{
static void main( string [] args)
{
list<gustinfo> glist = new list<gustinfo>()
{
new gustinfo(){ name= "debuglzq" ,age=26,tel= "187********" },
new gustinfo(){ name= "sarah" ,age=25,tel= "159********" },
new gustinfo(){ name= "jerry" ,age=35,tel= "135********" },
new gustinfo(){ name= "m&m" ,age=16,tel= "136********" },
new gustinfo(){ name= "debugman" ,age=26,tel= "136********" },
new gustinfo(){ name= "jerry&tom" ,age=19,tel= "136********" },
};
var query = from guest in glist
group guest by guest.name.substring(0, 1); //分组键key是string类型
//遍历键值和键值所属元素
foreach (igrouping< string , gustinfo> guestgroup in query)
{
console.writeline( "分组键:{0}" ,guestgroup.key );
foreach (var g in guestgroup)
{
console.writeline( "{0} 年龄:{1} 电话:{2}" ,g.name,g.age,g.tel );
}
}
console.readkey( false );
console.writeline( "-----------------------------------" );
var query2 = from guest in glist
group guest by guest.age > 20; //分组键key是bool类型表达式的结果
foreach (igrouping< bool , gustinfo> guestgroup in query2)
{
console.writeline( "年龄是否大于20 分组键:{0}" , guestgroup.key);
foreach (var g in guestgroup)
{
console.writeline( "{0} 年龄:{1} 电话:{2}" , g.name, g.age, g.tel);
}
}
console.readkey( false );
}
}
}
|
程序的运行结果如下:
5.into子句
into子句作为一个临时标识符,用于group、select、join子句中充当其结果的引用。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
using system;
using system.collections.generic;
using system.linq;
using system.text;
namespace linq_into
{
/// <summary>
/// linq group
/// </summary>
class program
{
static void main( string [] args)
{
list<gustinfo> glist = new list<gustinfo>()
{
new gustinfo(){ name= "debuglzq" ,age=26,tel= "187********" },
new gustinfo(){ name= "sarah" ,age=25,tel= "159********" },
new gustinfo(){ name= "jerry" ,age=35,tel= "135********" },
new gustinfo(){ name= "m&m" ,age=16,tel= "136********" },
new gustinfo(){ name= "debugman" ,age=26,tel= "136********" },
new gustinfo(){ name= "jerry&tom" ,age=19,tel= "136********" },
};
//1.into用于group子句
var query = from guest in glist
group guest by guest.name.substring(0, 1) into grguest
orderby grguest.key descending
select grguest;
var query2 = from guest in glist
group guest by guest.name.substring(0, 1) into grguest
orderby grguest.key ascending
select grguest;
//2.select 子句中的into子句
var query3 = from guest in glist
select new { newname = guest.name, newage = guest.age } into newguest
orderby newguest.newage
select newguest;
foreach (var guestgroup in query)
{
console.writeline( "分组键:{0}" ,guestgroup.key );
foreach (var g in guestgroup)
{
console.writeline( "{0} 电话:{1}" ,g.name,g.tel );
}
}
console.readkey( false );
foreach (var newg in query3)
{
console.writeline( "{0} 年龄:{1}" ,newg.newname,newg.newage );
}
console.readkey( false );
}
}
}
|
程序运行结果如下:
6.orderby子句、thenby子句
linq可以按照元素的一个或多个属性对元素进行排序。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
using system;
using system.collections.generic;
using system.linq;
using system.text;
namespace linq_orderby
{
class program
{
/// <summary>
/// linq orderby (ascending descending)
/// </summary>
/// <param name="args"></param>
static void main( string [] args)
{
list<gustinfo> glist = new list<gustinfo>()
{
new gustinfo(){ name= "debuglzq" ,age=26,tel= "187********" },
new gustinfo(){ name= "sarah" ,age=25,tel= "159********" },
new gustinfo(){ name= "jerry" ,age=35,tel= "135********" },
new gustinfo(){ name= "m&m" ,age=16,tel= "136********" },
new gustinfo(){ name= "debugman" ,age=26,tel= "136********" },
new gustinfo(){ name= "jerry&tom" ,age=19,tel= "136********" },
};
//按照年龄排序
var query = from guest in glist
orderby guest.age
select guest;
var query1 = from guest in glist
orderby guest.age ascending
select guest;
var query2 = from guest in glist
orderby guest.age descending
select guest;
//按照年龄进行排序,按照名字字数进行次要排序
var query3 = from guest in glist
orderby guest.age, guest.name.length
select guest;
var query4 = from guest in glist
orderby guest.age descending , guest.name.length ascending
select guest;
var query5 = from guest in glist
orderby guest.age, guest.name.length,guest.tel
select guest;
foreach (var guest in query2)
{
console.writeline( "{0} 年龄:{1} 电话:{2}" ,guest.name,guest.age,guest.tel );
}
console.readkey( false );
foreach (var guest in query4)
{
console.writeline( "{0} 年龄:{1} 电话:{2}" , guest.name, guest.age, guest.tel);
}
console.readkey( false );
}
}
}
|
程序运行结果如下:
7.let子句
let子句用于在linq表达式中存储子表达式的计算结果。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
using system;
using system.collections.generic;
using system.linq;
using system.text;
namespace linq_let
{
/// <summary>
/// linq let用来存子储表达式的计算结果
/// </summary>
class program
{
static void main( string [] args)
{
list<gustinfo> glist = new list<gustinfo>()
{
new gustinfo(){ name= "debuglzq" ,age=26,tel= "187********" },
new gustinfo(){ name= "sarah" ,age=25,tel= "159********" },
new gustinfo(){ name= "jerry" ,age=35,tel= "135********" },
new gustinfo(){ name= "m&m" ,age=16,tel= "136********" },
new gustinfo(){ name= "debugman" ,age=26,tel= "136********" },
new gustinfo(){ name= "jerry&tom" ,age=19,tel= "136********" },
};
//使用let子句创建范围变量g,并通过g构建查询表达式
var query = from guest in glist
let g = guest.name.substring(0, 1)
where g == "d" || g == "j"
select guest;
//也可以不使用let,上面的语句等效于下
var query2 = from guest in glist
where guest.name.substring(0, 1) == "d" || guest.name.substring(0, 1) == "j"
select guest;
foreach (var g in query)
{
console.writeline( "{0} 年龄:{1} 电话:{2}" ,g.name,g.age,g.tel );
}
console.readkey( false );
console.writeline( "不使用let,等效的语句结果" );
foreach (var g in query2)
{
console.writeline( "{0} 年龄:{1} 电话:{2}" , g.name, g.age, g.tel);
}
console.readkey( false );
}
}
}
|
程序的运行结果如下:
8.join子句
如果一个数据源中元素的某个属性可以跟另外一个数据源的中元素的某个属性进行相等比较,那么这两个数据源可以用join子句进行关联。
join子句使用equals关键字进行相等比较,而不是常用的双等号。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
using system;
using system.collections.generic;
using system.linq;
using system.text;
namespace linq_join
{
class program
{
static void main( string [] args)
{
//定义两个数据源
list<gustinfo> glist = new list<gustinfo>()
{
new gustinfo(){ name= "debuglzq" ,age=26,tel= "187********" },
new gustinfo(){ name= "sarah" ,age=25,tel= "159********" },
new gustinfo(){ name= "jerry" ,age=35,tel= "135********" },
new gustinfo(){ name= "m&m" ,age=16,tel= "136********" },
new gustinfo(){ name= "debugman" ,age=26,tel= "136********" },
new gustinfo(){ name= "jerry&tom" ,age=19,tel= "136********" },
};
list<guesttitle> titlelist = new list<guesttitle>()
{
new guesttitle(){name= "debuglzq" ,title= "soft engineer" },
new guesttitle(){name= "debuglzq" ,title= "team leader" },
new guesttitle(){name= "sarah" ,title= "test engineer" },
new guesttitle(){name= "jerry" ,title= "head master" }
};
//1.根据姓名进行内连接
var query = from guest in glist
join title in titlelist on guest.name equals title.name
select new { name=guest.name ,title=title.title,age=guest.age };
foreach (var g in query)
{
console.writeline( "{0} {1} 年龄:{2}" ,g.name,g.title ,g.age );
}
console.readkey( false );
//前面的多个from实现相同的作用:与内连接区别在于:这个中间的操作是叉乘获得笛卡尔积
var query2=from guest in glist
from title in titlelist
where guest.name==title.name
select new { name = guest.name, src="/uploads/allimg/211115/1435555948-7.jpg" />
延伸 · 阅读
精彩推荐
|