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. 3Linux常用命令

    文件目录管理命令 1.touch touch 文件名   #创建空白文件 -a  修改读取(访问)时间atime -m  修改修改时间mtime -d  同时修改atime 和 mtime touch ...

  2. python接收html页面上传的文件

    使用的 flask, 没有安装的先安装 pip install flask 示例代码:示例没有自动创建静态文件夹,需要自己在同级 创建一个名为 static 的文件夹来存放上传的文件 示例展示为图片 ...

  3. python中的多进程与多线程(一)

    进程是一个执行中的程序,每个进程有自己的地址空间.内存.数据栈以及其他用于跟踪执行的辅助数据.操作系统管理其上所有进程,并合理分配时间. 进程也可以通过fork或spawn派生新的进程,每个新进程有自 ...

  4. 剑指offer例题——反转链表

    题目描述 输入一个链表,反转链表,输出新链表的表头 程序编写 将链表反转 public class Solution { public ListNode ReverseList(ListNode he ...

  5. spring @transactional 注解事务

    1.在spring配置文件中引入<tx:>命名空间 <beans xmlns="http://www.springframework.org/schema/beans&qu ...

  6. 搭建mxnet-gpu docker的pyhon remote kernel

    起因 最近看mxnet的东西,打算给实验室的机器装一个mxnet的环境,无奈实验室里面机器已经装了tensorflow,运行了好久了,环境比较老.而mxnet可是支持最新的cuda9.1和cudnn7 ...

  7. user story

    What is a user story? A user story is a short description of something that your customer will do wh ...

  8. URL和URI

    (一)URL和URI是什么 1.URL(Universal Resource Locator) 是统一资源定位符,对可以从互联网上得到的资源的位置和访问方法的一种简洁的表示,是互联网上标准资源的地址. ...

  9. django 请求与响应

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. Python中使用%还是format来格式化字符串?

    Python中应该使用%还是format来格式化字符串?   %还是format Python中格式化字符串目前有两种阵营:%和format,我们应该选择哪种呢? 自从Python2.6引入了form ...