Joins allow developers to combine data from multiple tables into a sigle query.

Let's have a look at codes:

Creating a project

  1. Create a project named JoinTest

  2. Add Packages by NuGet

  3. 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; }
    }
  4. Execute commands:

    • Enable-Migrations
    • Add-Migration init
    • Update-Database
  5. 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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. Lerning Entity Framework 6 ------ Defining the Database Structure

    There are three ways to define the database structure by Entity Framework API. They are: Attributes ...

  6. Lerning Entity Framework 6 ------ Introduction to TPH

    Sometimes, you have created two models. They have the same parent class like this: public class Pers ...

  7. 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 ...

  8. Lerning Entity Framework 6 ------ Using a commandInterceptor

    Sometimes, We want to check the original sql statements. creating a commandInterceptor is a good way ...

  9. 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 ...

随机推荐

  1. VS2017打包设置

    本文为网络贴文,引用于:http://www.cnblogs.com/overstep/p/6942423.html 一.  安装打包插件: 安装打包插件:Microsoft Visual Studi ...

  2. CentOS开机自动运行自己的脚本详解

    一.root权限编辑/etc/rc.d/rc.local su cd /etc/rc.d/ vi rc.local 二.在这个文件加上你要执行的脚本,全部内容如下: #!/bin/sh # # Thi ...

  3. GDI+_入门教程【一】

    GDI For VisualBasic6.0 [一]文件下载:GDI+ For VB6[一] 简单绘图实例演示百度网盘 1 '以下为作者[vIsiaswx]的教程 '(该教程发布的原地址已无法访问,此 ...

  4. 基于maven javaweb编程缺少java源文件

    前提:基于maven的javaweb的开发环境,可参考 link 一. https://blog.csdn.net/ldlly0505/article/details/79674826 1.在sour ...

  5. Ubuntu下解决MySQL自启动,chkconfig list 全部off 情况

    chkconfig命令是用于RedHat/Fedora发行版的,而对于像Ubuntu之类的Debian发行版,应该使用这个命令: sudo update-rc.d mysql defaults 验证一 ...

  6. 接入层高性能缓存技术nginx+redis利器OpenResty

    一. OpenRestyOpenResty是一个基于 Nginx与 Lua的高性能 Web平台,其内部集成了大量精良的 Lua库.第三方模块以及大多数的依赖项.用于方便地搭建能够处理超高并发.扩展性极 ...

  7. linux 下将tomcat注册成服务并开机启动

    一.将startup.sh和shutdown.sh新建软连接到/usr/bin ln -s /usr/local/apache-tomcat-8.5.38/bin/startup.sh /usr/bi ...

  8. Cobbler安装CentOS7系统时报错 line 286: No space left on device

    原因分析: 此报错并不是说磁盘空间不足,而是内存太小了. CentOS7安装系统时最小要2G以上内存,而当时我只给了1G. 所有报错,把内存加大到2G后可以正常安装系统.

  9. IO高级应用关于字符码表

    ASCII码表: 计算机里只有数字,我在计算机软件里的一切都是用数字来表示,屏幕上显示的一个个字符也不例外.计算机诞生在美国,最开始所用到字符就是我们现在键盘上的一些符号和少数几个特殊的符号,每一个字 ...

  10. Windows Server 2012安装密钥

    Windows Server 2012 Standard 密钥:NB4WH-BBBYV-3MPPC-9RCMV-46XCB Windows Server 2012 StandardCore 密钥:NB ...