ASP EF框架,扩展函数和lambda表达式实现排序和分页


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class WebForm3 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        //扩展函数和lambda表达式实现查询。扩展函数实现排序。扩展函数Skip和Take实现分页
        protected void Button1_Click(object sender, EventArgs e)
        {
            //Func whereLambda = u => { return u.ID == 343; };

            EFFristModelEntities db = new EFFristModelEntities();
            //var userInfoList = db.UserInfo.Where(u=>u.ID==3);  //通过扩展函数where和lambda表达式来查询。泛型可以省略。

            //升序排序
            //var userInfoList = db.UserInfo.Where(U => true).OrderBy(u => u.ID);  //根据u.ID排序,int是根据u.ID的类型来判断的。<泛型>可以省略。
            //降序排序
            //var userInfoList = db.UserInfo.Where(U => true).OrderByDescending(u => u.ID);  //扩展函数OrderByDescending实现降序排序。
            //var userInfoList = db.UserInfo.Where(U => true).OrderByDescending(u => u.UserPass).ThenByDescending(u => u.ID);//扩展函数OrderByDescending,ThenByDescending实现排序。
            
            int pageIndex = 2;
            int pageSize = 2;
            var userInfoList = (from u in db.UserInfo
                                where u.ID > 0
                                orderby u.RegTime ascending, u.ID descending  //linq表达式实现排序。
                                select u).Skip((pageIndex - 1) * pageSize).Take(pageSize);  //分页:Skip:跳过多少条记录;Take:取几条记录。

            foreach (var userInfo in userInfoList)
            {
                Response.Write(userInfo.UserName + "
"); } } } }


你可能感兴趣的:(ASP.NET,C#)