如何使用join显示来自多个表的数据

时间:2011-07-29 05:07:33

标签: linq asp.net-mvc-3 entity-framework-4

专家,

现在我在 TabMasterController

中添加了以下代码
 private CAFMEntities db = new CAFMEntities();

        public ViewResult Index(string sortorder, int? pagesize, int? page)
        {
            int pageSize = pagesize ?? 10;
            if (Request.HttpMethod != "GET")
            {
                page = 1;
                pageSize = 10;
            }
            ViewBag.SelectedPageSize = pageSize;

            ViewBag.CurrentSort = sortorder;
            ViewBag.FirstNameSortParm = String.IsNullOrEmpty(sortorder) ? "FirstName desc" : "";
            ViewBag.LastNameSortParm = sortorder == "LastName" ? "LastName desc" : "LastName";
            ViewBag.DepNameSortParm = sortorder == "depName" ? "depName desc" : "depName";

            var joined = from tm in db.TabMasters
                         join tk in db.TabKeys on tm.colID equals tk.colID
                         join td in db.TabDeps on tk.depID equals td.depID
                         select new { tm.FirstName, tm.LastName, tk.colID, tk.depID, td.depName };
            switch (sortorder)
            {
                case "FirstName":
                    joined = joined.OrderBy(m => m.FirstName);
                    break;
                case "FirstName desc":
                    joined = joined.OrderByDescending(m => m.FirstName);
                    break;
                case "LastName":
                    joined = joined.OrderBy(m => m.LastName);
                    break;
                case "LastName desc":
                    joined = joined.OrderByDescending(m => m.LastName);
                    break;
                case "depName":
                    joined = joined.OrderBy(m => m.depName);
                    break;
                case "depName desc":
                    joined = joined.OrderByDescending(m => m.depName);
                    break;
                default:
                    joined = joined.OrderBy(m => m.FirstName);
                    break;
            }

            int pageIndex = (page ?? 1) - 1;
            int start = (pageIndex * pageSize);
            ViewBag.TotalRecord = joined.Count();
            ViewBag.StartRecord = start + 1;
            ViewBag.EndRecord = ((start + pageSize) >= ViewBag.TotalRecord) ? ViewBag.TotalRecord : (start + pageSize);
            return View(joined.ToPagedList(pageIndex, pageSize));
        }

已更新

namespace MVCMultilingual.Models
{
    public class TabMasterModel 
    {
        [ReadOnly(true)]
        public int colID { get; set; }
        [ReadOnly(true)]
        public int depID { get; set; }
        [DisplayName("FirstName")]
        public string FirstName { get; set; }
        [DisplayName("LastName")]
        public string LastName { get; set; }
        [DisplayName("depName")]
        public string depName { get; set; }
    }
}

@model PagedList.IPagedList<MVCMultilingual.Models.TabMasterModel>

但我不知道将查询输出设置为类

TabMasterController的索引页面的以下代码

@model PagedList.IPagedList<MVCMultilingual.TabMaster>
@using Helper
@{
    ViewBag.Title = "Index";
}
<h2>
    Index</h2>
<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table cellpadding="0" cellspacing="0" width="500">
    <tr class="row-head">
        <td>
            @Html.ActionLink("FirstName", "Index", new { sortOrder = ViewBag.FirstNameSortParm, pagesize = ViewBag.SelectedPageSize })
        </td>
        <td>
            @Html.ActionLink("LastName", "Index", new { sortOrder = ViewBag.LastNameSortParm, pagesize = ViewBag.SelectedPageSize })
        </td>
        <td>
            @Html.ActionLink("depName", "Index", new { sortOrder = ViewBag.DepNameSortParm, pagesize = ViewBag.SelectedPageSize })
        </td>
        <td>
        </td>
    </tr>
    @{
        foreach (var row in Model.Select((item, index) => new { index, item }))
        {
        @Html.Raw(GridHelper.BeginRow(row.index));
        <td>
            @Html.DisplayFor(modelItem => row.item.FirstName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => row.item.LastName)
        </td>
         <td>
            @Html.DisplayFor(modelItem => row.item.depName)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id = row.item.colID }) |
            @Html.ActionLink("Details", "Details", new { id = row.item.colID }) |
            @Html.ActionLink("Delete", "Delete", new { id = row.item.colID })
        </td>
        @Html.Raw(GridHelper.EndRow());
        }
    }
    @Html.Raw(GridHelper.BeginFooter(ViewContext.RouteData.Values["controller"].ToString(), 3, Convert.ToInt16(ViewBag.StartRecord), Convert.ToInt16(ViewBag.EndRecord), Convert.ToInt16(ViewBag.TotalRecord), Convert.ToInt16(Model.PageCount), Convert.ToInt16(Model.PageNumber), Convert.ToInt16(ViewBag.SelectedPageSize), ViewBag.CurrentSort, Model.HasPreviousPage, Model.HasNextPage))
    @Html.Raw(GridHelper.EndFooter())
  </table>

在这段代码中我使用了以下行

@model PagedList.IPagedList<MVCMultilingual.TabMaster>

现在我想在此页面中添加TabDep.depName,但它会给我一个错误

   @Html.DisplayFor(modelItem => row.item.depName)

错误: 'MVCMultilingual.TabMaster'不包含'depName'的定义,也没有扩展方法'depName'接受类型'MVCMultilingual.TabMaster'的第一个参数(你是否缺少using指令或汇编引用?)

2 个答案:

答案 0 :(得分:0)

首先你需要检查以确保加入!= 0,然后你不会尝试在未创建时使用数据/ I.E没有来自数据库的结果。你总是有一个排序顺序,所以你总是会进入开关。最后一个tabMaster是TabMaster表的模型,你创建的是一个新模型,所以你应该创建一个ViewModel并用数据填充新的View模型,然后通过视图模型循环你的记录,你会发现这个修复问题

select new MyViewModel{
     fname = tm.FirstName, 
     lname = tm.LastName, 
     ID = tk.colID, 
     DEP = tk.depID, 
     name = td.depName
 }

答案 1 :(得分:0)

你打电话给我:

select new { tm.FirstName, tm.LastName, tk.colID, tk.depID, td.depName };

您的结果不再是TabMaster。结果将变为匿名类型,并且期望TabMaster的所有内容都将失败。因此,如果您想使用投影并以强类型方式使用它,则必须投影到新类型 - 类似于TabMasterViewModel`。

顺便说一下。为什么要映射联结表?只需移除Id中的TabKey并制作ColIdDepId复合主键,您的模型就会简化,因为TabMaster之间会有直接的M:N关系和TabDep