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. SecureCRT介绍

    SecureCRT® combines rock-solid terminal(安全兼备可靠的终端) emulation with the strong encryption, broad range ...

  2. P1025[SCOI2009]游戏

    windy学会了一种游戏.对于1到N这N个数字,都有唯一且不同的1到N的数字与之对应.最开始windy把数字按 顺序1,2,3,……,N写一排在纸上.然后再在这一排下面写上它们对应的数字.然后又在新的 ...

  3. javascript实现数据结构: 树和森林

    树的3种常用链表结构 1 双亲表示法(顺序存储结构) 优点:parent(tree, x)操作可以在常量时间内实现 缺点:求结点的孩子时需要遍历整个结构 用一组连续的存储空间来存储树的结点,同时在每个 ...

  4. How To Secure Apache with Let's Encrypt on Ubuntu (Free SSL)

    Introduction This tutorial will show you how to set up a TLS/SSL certificate from Let's Encrypt on a ...

  5. zookeeper安装和配置(单机+伪集群+集群)

    #单机模式 解压到合适目录. 进入zookeeper目录下的conf子目录, 复制zoo_sample.cfg-->zoo.cfg(如果没有data和logs就新建):tickTime=2000 ...

  6. mac 下常用快捷键

    1.快速搜索某个类 双击thift 2.切换不同的类: ctrl+方向键 3.alt+command+B 进入到具体的子类 但是 ctrl+方向键一直切的是电脑上 桌面的切换.打开 系统偏好设置-快捷 ...

  7. 检查 NaN 数据值 (C/C++/Python 实现)

    NaN 是 Not a Number 的缩写.它是一个数值类型值,通常在浮点计算中,表示未定义或无法表示的值.而且,不能直接使用相等运算符 (==) 检查 NaN.由于在程序中,nan == nan ...

  8. spring cloud zuul 配置

    参考:http://www.ityouknow.com/springcloud/2017/06/01/gateway-service-zuul.html spring boot版本:2.0.3.REL ...

  9. Mycat中间件

    数据库中间件Mycat自我介绍 一.mycat概述 1.功能介绍 mycat一个开源的分布式数据库系统,是一个实现了mysql协议的server前端用户可以把它看成一个数据库代理,用mysql客户端工 ...

  10. cocos ide使用binding-generator导出来的c++类

    time:2015/03/19 cocos版本:3.2 描述:用了ide运行一个实例[1]的时候需要增加c++类,正确导出来之后,直接使用vs2012启动是没有问题的,但是使用ide启动却提示找不到模 ...