1.首先我們想讓列表頁顯示兩個表的共同數據

這裡有兩張表

public class mytype
    {
        public int mytypeID { get; set; }
        public string mytypeName { get; set; }
    }

public class Author
    {
        public int AuthorID { get; set; }
        public string AuthorName { get; set; }
    }

2.然後新建立一個新的文件夾,然後添加一個類

public class all
    {
        public int id { get; set; }

public string type { get; set; }

public string other { get; set; }

}

3.  public class mytryController : Controller
    {
        //
        // GET: /mytry/
        AllContext db = new AllContext();

public ActionResult Index() //在這裡我們獲取到了類all中的數據
        {
            var a = from i in db.Mytype
                    from j in db.Author
                    where i.mytypeID == j.AuthorID
                    select new all{ id=i.mytypeID, type=i.mytypeName, other=j.AuthorName };
            return View(a);
        }
    }

4.然後再對應的視圖頁面

@model IEnumerable<MvcApplication12.viewModel.all>

然後再下面下出,這樣你所需要的數據就出來了

@foreach (var item in Model)
    {
     @item.id
     @item.type
     @item.other
     }

現在還有一個類似的方法

首先添加一個all類

namespace MvcApplication12.viewModel
{
    public class all
    {
        public IEnumerable<Author> author { get; set; }
        public IEnumerable<mytype> mytype { get; set; }
    }
}

在控制器中

public class mytryController : Controller
    {
        //
        // GET: /mytry/
        AllContext db = new AllContext();
        viewModel.all al = new viewModel.all();
        
        public ActionResult Index() //在這裡我們獲取到了類all中的數據
        {
            var a = from i in db.Mytype
                    from j in db.Author
                    where i.mytypeID == j.AuthorID
                    select new myClass{Id=i.mytypeID,Type=i.mytypeName,Name=j.AuthorName};

return View(a);
        }
    }

public class myClass
    {
        public int Id { get; set; }
        public string Type { get; set; }
        public string Name { get; set; }
    }

然後在視圖頁面

@model IEnumerable<MvcApplication12.Controllers.myClass>

@foreach (var item in Model)
    {
    @item.Id
    @item.Name
    @item.Type
     }

這樣也是可以的。

ok

EF中連表查詢的應用方式的更多相关文章

  1. EF中多表公共字段,以及设置EntityBase使所有实体类继承自定义类

    使用EF框架访问数据库时,如果某些表具有公共字段,例如在审核流程中,对于各类申请单资料的创建人.创建时间.修改人.修改时间,这些可能多表都需要的字段,如果在每个实体中进行赋值操作显然是类似和重复的,下 ...

  2. Action 中获取表单数据的三种方式

    (尊重劳动成果,转载请注明出处:http://blog.csdn.net/qq_25827845/article/details/53138905  冷血之心的博客) Action 中获取表单提交数据 ...

  3. EF中新建表和关联表的方法

    以机场表为例 private static AIRPORT_HELIPORTManager AirportHeliportManager => ManagerFactory.Instance.A ...

  4. Day20-单表中获取表单数据的3种方式

    1. 搭建环境请参考:http://www.cnblogs.com/momo8238/p/7508677.html 2. 创建表结构 models.py from django.db import m ...

  5. 如何查詢 SQL Server 資料庫中欄位值為 NULL 的資料(转)

    最近使用mssql的时候对于未null的字段查询不到 http://blogs.msdn.com/b/jchiou/archive/2008/05/01/sql-server-null.aspx 先建 ...

  6. easyui datagrid 禁止选中行 EF的增删改查(转载) C# 获取用户IP地址(转载) MVC EF 执行SQL语句(转载) 在EF中执行SQL语句(转载) EF中使用SQL语句或存储过程 .net MVC使用Session验证用户登录 PowerDesigner 参照完整性约束(转载)

    easyui datagrid 禁止选中行   没有找到可以直接禁止的属性,但是找到两个间接禁止的方式. 方式一: //onClickRow: function (rowIndex, rowData) ...

  7. EF 操作数据库中的表

    1.VS创建项目(实现数据库的增删改查.并在dataGridView中展示) 增.改通过Button,删通过快捷菜单ContextMenuStrip控件(DateGridView控件的ContextM ...

  8. DB表的关系及EF中Fluent API的使用

    现在使用多数的数据库是关系型数据库,那么表与表之间的关系就会显得尤其重要,对于数据的CRUD处理和以后数据的分析有很大的好处.下面是对于数据库中对表关系的理解以及在EF中使用Fluent API来创建 ...

  9. oracle 查詢表字段明細、字段注釋、表註釋

    查詢表字段明細 select  column_name,data_type,data_length,DATA_PRECISION ,DATA_SCALE from all_tab_columns wh ...

随机推荐

  1. Shiro官方快速入门10min例子源码解析框架1-初始化

    Shiro,一个易用的Java安全框架,主要集合身份认证.授权.加密和session管理的功能. 这系文章主要简介Shiro架构,并通过官方的quickstart例程分析最简实现下Shiro的工作流程 ...

  2. hdu 1251 统计难题 字典树第一题。

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

  3. 4.5&4.7联考题解

    本来想加个密码的,后来一想全HE就咱们这几个人,外省的dalao愿看也没事儿,就公开算了,省得加密码各种麻烦. 先补这两天的题解吧……如果有空的话我可能会把上次联考的题解补上= =(中午没睡觉,现在困 ...

  4. css3在页面中插入内容

    A. 使用选择器来插入内容 h2:before{ content:"前缀"; } h2:after{ content:"后缀"; } B. 指定个别的元素不进行 ...

  5. Leetcode算法比赛---- Lexicographical Numbers

    问题描述 Given an integer n, return 1 - n in lexicographical order. For example, given 13, return: [1,10 ...

  6. Python实现冒泡,选择排序

    def bubble(num): for i in range(len(num)-1): for j in range(len(num)-i-1): if(num[j]>num[j+1]): t ...

  7. Install guide for OpenLDAP and GOsa 2 on Ubuntu & Debian

    First we will install OpenLDAP by running the command as root: apt-get install slapd ldap-utils ldap ...

  8. 一张图看懂 JS 原型链

    JS 原型链,画了张图,终于理清楚各种关系有木有 写在最后: __proto__是每个对象都有的一个属性,而prototype是函数才会有的属性!!! function Person() { } 是函 ...

  9. restful知识点之六rest-framework组件流程图

  10. 【Oracle】锁表处理 SQL 错误: ORA-00054: 资源正忙, 但指定以 NOWAIT 方式获取资源, 或者超时失效

    问题描述有时候ORACLE数据的某些表由于频繁操作,而且比较大,会导致锁表(死锁). 问题分析(1)锁的分析ORACLE里锁有以下几种模式:0:none1:null 空2:Row-S 行共享(RS): ...