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. nginx 安装配置和常用命令

    基本环境:CentOS 1. 下载 nginx [root@localhost ~]# cd Downloads/ [root@localhost Downloads]# wget http://ng ...

  2. poj 3070 Fibonacci 矩阵相乘

    Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7715   Accepted: 5474 Descrip ...

  3. ASP.NET MVC与ASP.NET WebForm

    ASP.NET MVC是微软公司的一款WEB开发框架,整合了“模型-视图-控制器”架构的高效与整洁,是敏捷开发最现代的思想与技术.它是传统ASP.NET WebForm的一个完善的替代品. 1.当今的 ...

  4. java 包装类和基础数据

    package com.tercher.demo; public class LangClass { public static void main(String[] args) { //所有的包装类 ...

  5. MyEclipse打包带源码的jar包

    平时开发中,我们喜欢将一些类打包成jar包,然后在别的项目中继续使用,不过由于看不到jar包里面的类的源码了,所以也就无法调试,要想调试,那么就只能通过关联源代码的形式,这样或多或少也有一些不方便,今 ...

  6. Qt Quick程序的发布

    要将程序发布出去,首先需要使用release方式编译程序,然后将生成的.exe可执行文件和需要的库文件发在一起打包进行发布. 要确定需要哪些动态库文件,可以直接双击.exe文件,提示缺少那个dll文件 ...

  7. JavaScript中map函数和filter的简单举例

    JavaScript的数组迭代器函数map和filter,可以遍历数组时产生新的数组,和python的map函数很类似 1> filter是满足条件的留下,是对原数组的过滤:2> map则 ...

  8. flask代码统计作业

    用户表: create table userInfo( id int not null unique auto_increment, name )not null, password ) not nu ...

  9. return, break and continue

    最近学习多线程看到了一个描述不太准确的地方,让我对return break continue个语句的不同开始详细了解了一番,之前只是知道怎么使用,不是很深入研究,基本遇到了跳出就用return来解决全 ...

  10. ffmpeg 简介及使用

    简介 ffmpeg [global_options] {[input_file_options] -i input_url} ... {[output_file_options] output_url ...