码迷,mamicode.com
首页 > 其他好文 > 详细

entity framework 新手入门篇(3)-entity framework实现orderby,count,groupby,like,in,分页等

时间:2014-12-21 23:36:02      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:

前面我们已经学习了entityframework的基本的增删改查,今天,我们将在EF中实现一些更加贴近于实际功能的SQL方法。

承接上面的部分,我们有一个叫做House的数据库,其中包含house表和seller表。

在本次学习之前,我们先要了解一个神奇的接口,iqueryable这个接口类似于ienumable,但并不完全相同,Iqueryable是可查询类型需要实现的最重要的接口,在其Count,ToList,ToArray之后才会真正执行查询,所以,为了保证性能,请尽量在最后一步在进行Count,ToList,ToArray等操作。

一、entity framework 中的orderby。

        public static List<House> GetListOrderByPrice()
        {
            List<House> houseList = null;
            using (HouseEntities db = new HouseEntities())
            {
                houseList = db.House.OrderBy(x => x.Price).ToList();
                //orderby方法是iqueryable类型对象的实例方法,也就是说,在where,select等方法后,都可以直接使用orderby来进行排序
                houseList = db.House.OrderByDescending(x => x.Price).ToList();
                //如果是逆序,请使用OrderByDescending方法
            }
            return houseList;
        }

orderby方法就是EF的排序方法,上面的代码展示了如何提取根据价格排序取房屋列表的方法。

二、entity framework 实现计数

        public static int GetCount()
        {
            int total = 0;
            using (HouseEntities db = new HouseEntities())
            {
                total = db.House.Count();
                //count方法即可实现类似SQL的count(1)方法,此方法可计算符合条件的数据条数。
                //count方法也是iqueryable类型对象的实例方法,所以,可以接在where,select等方法后使用,如下所示,即可查询所有1层的房屋
                total = db.House.Where(x => x.Floor == 1).Count();
            }
            return total;
        }

使用count方法即可轻松获得数量。

三、entity framework 实现groupby

        public static List<String> GetGroupByRegion()
        {
            List<string> groupByList = null;
            using (HouseEntities db = new HouseEntities())
            {
                groupByList = db.House.GroupBy(x => x.Region).Select(x => x.Key).ToList();
                //GroupBy方法即可实现SQL的Group By关键字,此方法可对已有数据进行分类,需要注意的是,在groupby之后,还需要选择key作为groupby之后的字段来进行返回
                //GroupBy方法也是iqueryable类型对象的实例方法,所以,可以接在where,select等方法后使用
                //上面的例子,也可以写成ToArray,这样会返回一个字符串数组,相对开销更小,但数组长度不可变,如需对其进行扩增相对困难。
            }
            return groupByList;
        }

使用上面的方法即可实现groupby。

四、entity framework 实现like和in

       public static List<House> GetListOrderByPrice(string region)
        {
            List<House> houseList = null;
            using (HouseEntities db = new HouseEntities())
            {
                houseList = db.House.Where(x=>x.Region.Contains(region)).ToList();
                //Contains方法是string类型对象的实例方法,在EF中,它会被最终解析为 like‘% %’的样子
                houseList = db.House.Where(x => x.Region.StartsWith(region)).ToList();
                houseList = db.House.Where(x => x.Region.EndsWith(region)).ToList();
                //如果需要实现like‘% ’或者 like ‘ %’则需要使用 startwith,和endwith

                var ids = new int[] { 1, 2, 3 };
                houseList = db.House.Where(x => ids.Contains(x.ID)).ToList();
                //除了like以外,数组的Contains还可以实现 SQL中IN的操作。上面的代码既实现了 select * from house where id in (1,2,3)

            }
            return houseList;
        }

contains方法对于数组和字符串来说,可转换为不同的SQL关键字,方便我们实现LIKE和IN的操作。

五、entity framework 实现分页

        public static List<House> GetListByPage(int pagesize,int pageindex,out int count)
        {
            List<House> houseList = null;
            using (HouseEntities db = new HouseEntities())
            {
                count = db.House.Count();
                houseList = db.House.Skip((pageindex-1)*pagesize).Take(pagesize).ToList();
                //skip方法即可实现在列表中跳过N条的操作,take则可实现从当前索引开始提取N条的操作。
                //所以如上方法既实现了简单的分页操作,count则返回house表的真实数量。
            }
            return houseList;
        }

 

entity framework 新手入门篇(3)-entity framework实现orderby,count,groupby,like,in,分页等

标签:

原文地址:http://www.cnblogs.com/Damos/p/EF_3.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!