asp.net MVC + linq to Entity简单教程(四)linq to Entity查询中其它方法的使用
来源:互联网 发布:python下载安装32位 编辑:程序博客网 时间:2024/05/26 19:16
在前面我们说到了,简单的列表显示,下面我们来介绍一下,一些其它的属性。
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_studentslinq 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后面匹配。如下
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.EndsWith(schoolcode) orderby s.studentID descending select s;
第三种 :前面是cg后面匹配
var stu = from s in newEtrain.ETrain_students where s.schoolcode.StartsWith(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
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中你可以进行合计函数的运算。
- asp.net MVC + linq to Entity简单教程(四)linq to Entity查询中其它方法的使用
- asp.net MVC + linq to Entity简单教程(四)linq to Entity查询中其它方法的使用
- asp.net MVC + linq to Entity简单教程(五)linq to Entity中join的使用以及子查询
- asp.net MVC + linq to Entity简单教程(五)linq to Entity中join的使用以及子查询
- asp.net MVC + linq to Entity简单教程(三)linq to Entity的基本操作
- asp.net MVC + linq to Entity简单教程(三)linq to Entity的基本操作
- asp.net MVC + linq to Entity简单教程(二)MVC的简单操作
- asp.net MVC + linq to Entity简单教程(二)MVC的简单操作
- asp.net MVC + linq to Entity简单教程(一)MVC的建立与Entity的建立
- asp.net MVC + linq to Entity简单教程(一)MVC的建立与Entity的建立
- asp.net MVC + linq to Entity linq to Entity的基本操作
- asp.net mvc linq to entity 查询指定列的数据以json字符串格式返回数据
- LINQ to Entity 动态查询
- linq to entity查询的日期格式化
- asp.net mvc2,linq to entity的创建,编辑,删除
- linq to entity(两个entity的连接查询)
- Entity framewrok (linq to entity)查询优化的一点摸索
- Linq to Entity 的问题
- Ubuntu维护日志
- jquery插件之滑块
- struts2+fileupload
- oracle_rollup和cube函数使用心得
- 如何使用电脑
- asp.net MVC + linq to Entity简单教程(四)linq to Entity查询中其它方法的使用
- XP系统用VMWare构建iPhone开发环境
- 有关RS232和RS485接口的问答
- 经历JMS编程
- 实战串行通讯
- java 模拟公告板 wait -notify 实例
- 面向 Java 开发人员的 Ajax: Google Web Toolkit 入门
- View编程(9): TypeArray、attrs.xml
- asp.net 内联表达式