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

    public class Solution { public int LengthOfLongestSubstring(string s) { var dic = new Dictionary< ...

  2. C# String 与 StringBuilder

    String 字符串不可变性,每次为字符串进行增删或重写赋值会销毁原来的字符串,重新开辟内存空间,因此是非常消耗资源的 字符串可以看做是 char 数组,因此可以用 foreach 对其进行遍历,或者 ...

  3. python练习题_01

    1.执行python的两种方式 答:1.通过解释器执行1.py 2.通过cmd执行python,再执行1.py 2.简述位与字节的的关系 答:8位=1字节(计算机处理时以字节为单位,存储时以位为单位) ...

  4. MySql:SELECT 语句(一)基本查询

    1.检索单个列 语句:SELECT col FROM tablename; 2.检索多个列 语句:SELECT col1, col2 FROM tablename; 3.检索所有列 使用 * 通配符. ...

  5. Helm简介

    什么是Helm 微服务和容器化给复杂应用部署与管理带来了极大的挑战.Helm是目前Kubernetes服务编排领域的唯一开源子项目,作为Kubernetes应用的一个包管理工具,可理解为Kuberne ...

  6. ReactiveX 学习笔记(21)使用 Rx.NET + ReactiveUI 进行 GUI 编程

    课题 程序界面由3个文本编辑框和1个文本标签组成. 要求文本标签实时显示3个文本编辑框所输入的数字之和. 文本编辑框输入的不是合法数字时,将其值视为0. 3个文本编辑框的初值分别为1,2,3. 创建工 ...

  7. Controller层aop

    利用@Around通知修改Controller的返回值 自定义一个注解@OperationBtn 在切入点Controller上加上自定义注解 接下来就是重点了,AspectJ写切面类,对该Contr ...

  8. faster rcnn源码阅读笔记1

    自己保存的源码阅读笔记哈 faster rcnn 的主要识别过程(粗略) (开始填坑了): 一张3通道,1600*1600图像输入中,经过特征提取网络,得到100*100*512的feature ma ...

  9. extJs相关名字解释

      1.initComponent 初始化部件启动 2.defaults : Object defaults属性可以包含任意个name/value属性对,这些属性将会被添加到每一个元素中...例如, ...

  10. 9.22 keep studying

    项目要换用element组件了. element [http://element.eleme.io/1.4/#/zh-CN]是饿了么前端出品的一套基于Vue2.0的组件库,今天看了一下,确实简单好看. ...