在前面我们说到了,简单的列表显示,下面我们来介绍一下,一些其它的属性。
1, order by 的使用
在sql语句中,order by 语句是经常使用的。一般来说,它一般和top配对使用。这里我们介绍一下linq to Entity中如何使用order by
一般正常的sql语句应该是
- select top 10 * from table order by id desc
在linq to Entity中的写法是
- //这里要注意的一点是这里没有top 10的功能
- var stu = from s in newEtrain.ETrain_students
- where s.schoolcode == schoolcode && s.sortcode == sortcode
- orderby s.studentID descending
- select s;
- //如果想加上top 10的功能,我们就要用到Take这个方法了,这个方法,一般情况下还会与Skip方法连用,用于分页
- var stu = (from s in newEtrain.ETrain_students
- where s.schoolcode == schoolcode && s.sortcode == sortcode
- orderby s.studentID descending
- select s).Take(10);
- //这里要注意一点的是,orderby可以独立使用,可是Take却必须要和orderby合用,不然会报错。
-
- 如果我是有二个字段要进行排充怎么办呢,只要用逗号隔开就行了,
- 下面就是按studentID的倒排和学校名字正排
- var stu = (from s in newEtrain.ETrain_students
- where s.schoolcode == schoolcode && s.sortcode == sortcode
- orderby s.studentID descending,s.schoolname
- select s).Take(10);
- 2, Distinct的使用
- Distinct一般用来取唯一性数据
- sql语句如下:
- SELECT DISTINCT schoolcode,stu_name FROM ETrain_students
- linq to Entity的写法就很意思了。要写在外面,这里说明一下,我这里用到的是最简单的单字段方法,如果要多字段比较,就得重载一下他的方法。这个在以后会提到
-
- var stu = (from s in newEtrain.ETrain_students
- where s.schoolcode == schoolcode && s.sortcode == sortcode
- select s.stu_name).Distinct();
- 只所以把Distinct拿到这里说,是因为Distinct在与orderby联合使用的时候,会有一个顺序问题。
-
- //这样写,你会发现orderby根本就没有起作用
- var stu = (from s in newEtrain.ETrain_students
- where s.schoolcode == schoolcode && s.sortcode == sortcode
- orderby s.studentID ,s.schoolname
- select s.stu_name).Distinct();
- //为什么会这样呢,还记得上一节中,我们提到的sql运行顺序呢。distinct正常的执行顺序是在orderby之前,所以正确的写法是
- var stu=(from s in newEtrian.ETrain_students
- where s.schoolcode==schoolcode && s.sortcode==sortcode
- select s.stu_name).Distinct().Orderby(p=>p.studentID);
-
- 把顺序变一下就好了,这里面的OrderBy()上正排序,.OrderByDescending()是用来倒排它是二个方法。这里有一个问题如果我有二个排序字段怎么办。linq to Entity给出了一个很有意思的方法orderby().thenby()。它是先按orderby进行排序之后,再按thenby进行排序
- 3, like的使用
- 在sql中查询使用最多的方法,就是like了。它一般有三种方式第一种,就是中间是cg两头匹配,第二种是后面是cg前面匹配,第三种是前面是cg后面匹配。如下<br>
- Selec * from table where id like '%cg%'
- Selec * from table where id like '%cg'
- Selec * from table where id like 'cg%'
- 在linq to Entity中给出了三种方法来实现来一一对应上面的方法
- 第一种:就是中间是cg两头匹配
- var stu = from s in newEtrain.ETrain_students
- where s.schoolcode.Contains(schoolcode)
- orderby s.studentID descending
- select s;
- 第二种:后面是cg前面匹配
- var stu = from s in newEtrain.ETrain_students
- where s.schoolcode.<span style="font-family:Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif;font-size:12px;line-height: 18px; ">EndsWith</span>(schoolcode)
- orderby s.studentID descending
- select s;
- 第三种 :前面是cg后面匹配
-
- var stu = from s in newEtrain.ETrain_students
- where s.schoolcode.<span style="font-family:Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif;font-size:12px;line-height: 18px; ">StartsWith</span>(schoolcode)
- orderby s.studentID descending
- select s;
- 4, group by的使用
- group by 语句用于结合合计函数,根据一个或多个列对结果集进行分组
- sql 语句写法如下:
- SELECT studentid,stu_name FROM ETrain_students GROUP BY stu_name,studentid</pre><br>
- linq to Entity的写法如下
- var stu = from s in newEtrain.ETrain_students
- where s.schoolcode==schoolcode
- group stu_name by new {studentid,stu_name} into stus
- select new stu_count{
- stuid= stus.studentid,
- stuname = stus.stu_name,
- stucount = stus.count()
- };
- 这里面group 后面跟的是你要显示的相应字段,这里我们要显示是学生的姓名, by 后面则是你要按什么来进行分类的字段。into表示,你将分好类的数据存入到其中。
- 在select中你可以进行合计函数的运算。
-
- </pre>