標籤:des cPage style blog http ar io color os
題前:,Select new{"需要的欄位列.."}好處,減少不必要資料的查詢,尤其是分布式的時候,網路再不好的情況下,而不必要的資料又很多,Select new{"需要的欄位列.."}好處明顯
如題,Linq多錶鏈接分頁,Select new{"需要的欄位列.."},配合楊濤Mvcpager,前台遍曆展示自訂欄位
(1) 我的例子是,(Tb_Mnager)管理員表與(Tb_Role)角色表聯查,返回兩個表部分欄位的組合
如下控制器代碼:
public ActionResult List(int? pager) { pager = pager ?? 1; int pageSize =20; MyMvcCmsEntities db = new MyMvcCmsEntities(); //多錶鏈接查詢,Select new{}返回需要的欄位 //注意此處,rs實際會是一個List<Object>對象,如何在視圖中遍曆???答:反射 var rs = (from m in db.Tb_Manager join r in db.Tb_Role on m.roleId equals r.id orderby m.sequeNum descending select new { m.id, //自訂的狀態,sql語句會轉化為case..when..then status = m.isDisable == 1 ? "<font color='red'>禁用</font>" : "<font color='green'>正常</font>", m.loginName, m.sequeNum, m.date, //角色表中的角色名稱字 r.roleName }); //查詢總數----sql事件探查器可知:此處會查詢一次資料庫 ViewBag.allCount = rs.Count(); //本語句會自動產生select top....,並把結果List<Object>返回----此處會查詢一次資料庫 ViewBag.list = rs.ToPagedList((int)pager, pageSize); return View(); }
(2) 好了,結果返回了,那麼前台如何遍曆遍曆List<Object>的值呢?
答案:反射 詳情 C#反射的簡單應用
此處,我用反射原理,寫了一個協助類:
using System;using System.Collections.Generic;using System.Linq;using System.Reflection;using System.Text;using System.Threading.Tasks;namespace Mvc.Util{ public class Reflection { /// <summary> /// 擷取指定屬性的值(不區分大小寫) /// </summary> /// <param name="PropertyName">屬性名稱</param> /// <param name="o">目標對象</param> /// <returns></returns> public static Object GetPropertyValueByName(string PropertyName, Object o) { if (o == null) { o = new { }; } //建立一個返回對象 Object returnObject = new Object(); PropertyInfo[] p1 = o.GetType().GetProperties(); foreach (PropertyInfo pi in p1) { if (pi.Name.ToLower() == PropertyName.ToLower()) { returnObject = pi.GetValue(o); } } return returnObject; } }}
(3) 這就好辦了,那麼我們在視圖View中遍曆並讀取值就簡單多了:
@{int index = 0;} @foreach (Object t in ViewBag.list) { index++; <tr> <td align="center"><span class="checkall" style="vertical-align:middle;"><input class="list-box" value="@Reflection.GetPropertyValueByName("id", t).ToString()" type="checkbox" /></span></td> <td>@index</td> <td>@Reflection.GetPropertyValueByName("loginName", t).ToString()</td> <td> @Reflection.GetPropertyValueByName("roleName", t).ToString() </td> <td>@Reflection.GetPropertyValueByName("date", t).ToString()</td> <td> @Html.Raw(Reflection.GetPropertyValueByName("status", t).ToString()) </td> <td> <input type="text" value="@Reflection.GetPropertyValueByName("sequeNum", t).ToString()" class="sort" /> </td> <td align="center"><a href='@Url.Action("edit", "manager", new { area="cms",id=Reflection.GetPropertyValueByName("id", t).ToString()})'>修改</a></td> </tr> }
視圖中分頁代碼,注意Html.Pager擴充方法第一個參數的類型為IPagedList:
@Html.Pager((IPagedList)ViewBag.list, new PagerOptions { PageIndexParameterName = "pager", ShowPageIndexBox = false, PageIndexBoxType = PageIndexBoxType.TextBox, ShowGoButton = false, FirstPageText = "首頁", LastPageText = "尾頁", ShowFirstLast = true, CurrentPagerItemWrapperFormatString = "<span class=\"current\">{0}</span>", CssClass = "default" }, new { @style = "width:100%;float=left;" })
Linq多錶鏈接分頁,Select new{"需要的欄位列.."},配合楊濤Mvcpager,前台遍曆展示自訂欄位