题前:,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();
}
好了,结果返回了,那么前台如何遍历遍历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;" })