EF中連表查詢的應用方式
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中連表查詢的應用方式的更多相关文章
- EF中多表公共字段,以及设置EntityBase使所有实体类继承自定义类
使用EF框架访问数据库时,如果某些表具有公共字段,例如在审核流程中,对于各类申请单资料的创建人.创建时间.修改人.修改时间,这些可能多表都需要的字段,如果在每个实体中进行赋值操作显然是类似和重复的,下 ...
- Action 中获取表单数据的三种方式
(尊重劳动成果,转载请注明出处:http://blog.csdn.net/qq_25827845/article/details/53138905 冷血之心的博客) Action 中获取表单提交数据 ...
- EF中新建表和关联表的方法
以机场表为例 private static AIRPORT_HELIPORTManager AirportHeliportManager => ManagerFactory.Instance.A ...
- Day20-单表中获取表单数据的3种方式
1. 搭建环境请参考:http://www.cnblogs.com/momo8238/p/7508677.html 2. 创建表结构 models.py from django.db import m ...
- 如何查詢 SQL Server 資料庫中欄位值為 NULL 的資料(转)
最近使用mssql的时候对于未null的字段查询不到 http://blogs.msdn.com/b/jchiou/archive/2008/05/01/sql-server-null.aspx 先建 ...
- easyui datagrid 禁止选中行 EF的增删改查(转载) C# 获取用户IP地址(转载) MVC EF 执行SQL语句(转载) 在EF中执行SQL语句(转载) EF中使用SQL语句或存储过程 .net MVC使用Session验证用户登录 PowerDesigner 参照完整性约束(转载)
easyui datagrid 禁止选中行 没有找到可以直接禁止的属性,但是找到两个间接禁止的方式. 方式一: //onClickRow: function (rowIndex, rowData) ...
- EF 操作数据库中的表
1.VS创建项目(实现数据库的增删改查.并在dataGridView中展示) 增.改通过Button,删通过快捷菜单ContextMenuStrip控件(DateGridView控件的ContextM ...
- DB表的关系及EF中Fluent API的使用
现在使用多数的数据库是关系型数据库,那么表与表之间的关系就会显得尤其重要,对于数据的CRUD处理和以后数据的分析有很大的好处.下面是对于数据库中对表关系的理解以及在EF中使用Fluent API来创建 ...
- oracle 查詢表字段明細、字段注釋、表註釋
查詢表字段明細 select column_name,data_type,data_length,DATA_PRECISION ,DATA_SCALE from all_tab_columns wh ...
随机推荐
- 五:SpringCloud-Zuul
九:zuul路由网关 1.概述 1.1 是什么 Zuul包含了对请求的路由和过滤两个最主要的功能: 其中==路由功能==负责将外部请求转发到具体的微服务实例上,是实现外部访问统一入口的基础. 而==过 ...
- 【SSH网上商城项目实战02】基本增删查改、Service和Action的抽取以及使用注解替换xml
转自:https://blog.csdn.net/eson_15/article/details/51297698 上一节我们搭建好了Struts2.Hibernate和Spring的开发环境,并成功 ...
- SQL Server 2008各版本介绍区别(包含企业版 开发者版 标准版 Web版 工作组版 Express版 Compact版)
SQL Server 2008分为SQL Server 2008企业版.标准版.工作组版.Web版.开发者版.Express版.Compact 3.5版,其功能和作用也各不相同,其中SQL Serve ...
- C++学习笔记(2)----类模板和友元
当一个类包含一个友元声明时,类与友元各自是否是模板是相互无关的.如果一个类模板包含一个非模板友元,则友元被授权可以访问所有模板实例.如果友元自身是模板,类可以授权给所有友元模板实例,也可以只授权给特定 ...
- github连接提示
解决办法: 1,将连接方式从http更换为ssh.注意,github.com后面一定有(冒号): git remote rm origin git remote add origingit@githu ...
- 毕向东_Java基础视频教程第21天_IO流(1)
第21天-01-IO流(对象的序列化) ObjectInputStream与ObjectOutputStream 被操作的对象需要实现Serializable接口(标记接口) 非必须, 但强烈建议所有 ...
- C#.NET初识
1..NET平台特点 1)支持多种编程语言(C#.VB.F#.JavaScript.C++/CLI)-托管语言,此外还有Smaltalk.Ruby.Python.COBOL和Pascal的.NET编译 ...
- 从零开始——JSON ARRAY&JSON OBJECT
在学习“基于角色的权限”的例子中,遇到了json object和json array,因此在一番学习之后对此要点进行粗略整理. 参考: https://my.oschina.net/u/2601842 ...
- leetcode Ch2-Dynamic Programming I
一. 1. Edit Distance class Solution { public: int minDistance(string t1,string t2) { int len1=t1.size ...
- [翻译] LTInfiniteScrollView
LTInfiniteScrollView 效果: Usage - 使用 Create the scroll view by: 通过以下方式来创建出scroll view self.scrollView ...