entity framework 新手入門篇(3)-entity framework實現orderby,count,groupby,like,in,分頁等


前面我們已經學習了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;
        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM