Lerning Entity Framework 6 ------ Joins and Left outer Joins
Joins allow developers to combine data from multiple tables into a sigle query.
Let's have a look at codes:
Creating a project
Create a project named JoinTest
Add Packages by NuGet
Create entities:
public class Person
{
public int PersonId { get; set; } [MaxLength(50)]
public string Name { get; set; } public virtual PersonType PersonType { get; set; }
} public class PersonType
{
public int PersonTypeId { get; set; } public string PersonTypeName { get; set; }
} public class MyContext:DbContext
{
public MyContext():base("name=Test")
{ } public DbSet<PersonType> PersonTypes { get; set; } public DbSet<Person> People { get; set; }
}
Execute commands:
- Enable-Migrations
- Add-Migration init
- Update-Database
Add some test data by coding:
static void Main(string[] args)
{
AddTestData();
} private static void AddTestData()
{
using (MyContext context = new MyContext())
{
PersonType student = new PersonType();
student.PersonTypeName = "学生"; PersonType worker = new PersonType();
worker.PersonTypeName = "工人"; Person p1 = new Person();
p1.Name = "王进喜";
p1.PersonType = worker; Person p2 = new Person();
p2.Name = "柴玲";
p2.PersonType = student; Person p3 = new Person();
p3.Name = "完颜亮"; context.People.Add(p1);
context.People.Add(p2);
context.People.Add(p3);
context.SaveChanges();
}
}
}
using joins
static void Main(string[] args)
{
//AddTestData();
using (MyContext db = new MyContext())
{
var result = from p in db.People
join t in db.PersonTypes
on p.PersonType.PersonTypeId equals t.PersonTypeId
select new { Name = p.Name, Type = t.PersonTypeName };
foreach (var item in result)
{
Console.WriteLine(item);
}
}
Console.ReadLine();
}

using Left outer joins
static void Main(string[] args)
{
//AddTestData();
using (MyContext db = new MyContext())
{
var result = from p in db.People
join t in db.PersonTypes
on p.PersonType.PersonTypeId equals t.PersonTypeId into finalGroup
from groupData in finalGroup.DefaultIfEmpty()
select new { Name = p.Name, Type = groupData.PersonTypeName??"Unknown" };
foreach (var item in result)
{
Console.WriteLine(item);
}
}
Console.ReadLine();

I think this tructure is hard to understand, but it's useful.
That's all.
Lerning Entity Framework 6 ------ Joins and Left outer Joins的更多相关文章
- Lerning Entity Framework 6 ------ Defining Relationships
There are three types of relationships in database. They are: One-to-Many One-to-One Many-to-Many Th ...
- Lerning Entity Framework 6 ------ Handling concurrency With SQL Server Database
The default Way to handle concurrency of Entity Framework is using optimistic concurrency. When two ...
- Lerning Entity Framework 6 ------ Working with in-memory data
Sometimes, you need to find some data in an existing context instead of the database. By befault, En ...
- Lerning Entity Framework 6 ------ Inserting, Querying, Updating, and Deleting Data
Creating Entities First of all, Let's create some entities to have a test. Create a project Add foll ...
- Lerning Entity Framework 6 ------ Defining the Database Structure
There are three ways to define the database structure by Entity Framework API. They are: Attributes ...
- Lerning Entity Framework 6 ------ Introduction to TPH
Sometimes, you have created two models. They have the same parent class like this: public class Pers ...
- Lerning Entity Framework 6 ------ Complex types
Complex types are classes that map to a subset of columns of a table.They don't contains key. They a ...
- Lerning Entity Framework 6 ------ Using a commandInterceptor
Sometimes, We want to check the original sql statements. creating a commandInterceptor is a good way ...
- Lerning Entity Framework 6 ------ A demo of using Entity framework with MySql
Create a new project named MySqlTest Install following packages by right-clicking on the References ...
随机推荐
- python3 pyinstaller生成exe文件过程问题解决记录
1.使用pip安装pyinstaller 2.在cmd打开需生成可执行文件的python文件所在文件夹 3.使用命令pyinstaller -F -w **.py (代码中有import其他模块的,只 ...
- BOUNDARIES AND SPACE
BOUNDARIES AND SPACE Review Nice work! You've learned a lot. Let's review the web and CSS concepts c ...
- 尚硅谷springboot学习32-整合druid
使用druid数据源 配置druid数据源 这里配置的数据源参数并不会生效,因为在DataSourceProperties中并没有这些字段,如果想要生效,必须自己配置druid数据源 @Configu ...
- 一个RTSP/RTP over TCP 的丢包引起的问题
背景知识:可以查看https://www.cnblogs.com/lidabo/p/4483497.html RTSP/RTP over TCP TCP承载RTSP/RTP When you us ...
- Android Studio2.0 教程从入门到精通Windows版 - 提高篇
第二篇我们开发了一个Hello World应用,并介绍Android Sutdio的界面和如何调试应用,接下来将介绍一些常用的快捷键和必备插件. 常用快捷键 代码跳转 描述:跳转是为了方便代码位置的定 ...
- cacti监控服务器
1.Cacti基本概念详解 Cacti是用php语言实现的一个软件,它的主要功能是用snmp服务获取数据,然后用rrdtool储存和更新数据,当用户需要查看数据的时候用rrdtool生成图表呈现给用户 ...
- spring cloud 总结
Finchley版本Spring Cloud Finchley; Spring Boot 2.0.3 史上最简单的 SpringCloud 教程 | 第一篇: 服务的注册与发现(Eureka)(Fin ...
- hello1以及hello2的部分代码分析
(一)1.GreetingServlet.java源码文件: @WebServlet("/greeting") //以@WebServlet注释开头,注释指定相对于上下文根的URL ...
- php利用simple_html_dom类,获取页面内容,充当爬虫角色
PHP脚本扮演爬虫的角色,可能大家第一时间想到可能会是会正则,个人对正则的规则老是记不住,表示比较难下手,今天工作中有个需求需要爬取某个网站上的一些门店信息 无意间在网上看到一个比较好的类库叫:sim ...
- RFID数据清洗与数据清洗的区别
RFID数据清洗和一般数据清洗的不同: RFID数据清洗已经跨越到硬件范畴!造成脏数据的原因是硬件原理和硬件所处环境本身!要提高RFID数据清洗能力,就必须同时研究技术原理和环境本身之间的互动关系,而 ...