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

    /** * Definition for singly-linked list. * public class ListNode { * public int val; * public ListNo ...

  2. A CLOSER LOOK AT CSS

    A CLOSER LOOK AT CSS css-review Congratulations! You worked hard and made it to the end of a challen ...

  3. R语言-散点图进阶

    1.分组散点图 ①xyplot()函数 > library(lattice) > xyplot(mpg~disp, #定义Y~X轴 + data=mtcars, + groups=cyl, ...

  4. cdnbest里站点域名不同步到节点,报400错误的一般原因

    报400错误一般是站点里的域名没有同步到节点上面的原因,产生的原因一般是下面两点原因: 1.检查节点列表如下图所示的状态是否打钩,这是节点和主控的通信状态,打叉表示连接有问题 这里打叉的几种原因(1) ...

  5. 大数据入门到精通12--spark dataframe 注册成hive 的临时表

    一.获得最初的数据并形成dataframe val ny= sc.textFile("data/new_york/")val header=ny.firstval filterNY ...

  6. DB2(Procedure)存储过程遍历循环!

    有时候一些复杂的业务逻辑将要通过存储过程的循环语句进行处理;以下列出2种DB2存储过程的循环语句,方便以后的查看并使用! 推荐第一种方式的使用,最大的优点就是比较直观;在需要操作很多字段的情况下,不需 ...

  7. json11阅读

    概要:json11是一个基于c++11的json解析库,dropbox出品. 使用 直接举自带单元测试test.cpp中的例子: const string simple_test = R"( ...

  8. Windows驱动开发调试工具

    [开发工具] VS2012 [调试工具] Windbg:和VM配合实现双机联合调试,完成双机调试功能,可以结合<软件调试>这本书对Windbg有较为深入的认识. DebugView: 可以 ...

  9. redis在windows系统下的安装和两个问题

    今天首次接触redis,遇到一些问题,查了一些资料,在这里汇总整理下. redis的安装看这个:http://www.runoob.com/redis/redis-install.html. 问题1: ...

  10. jstl标准标签库 常用标签

    JSTL(JSP Standard Tag Library)标准标签库: 1, 核心标签(最常用, 最重要的) 表达式控制标签 out 输出常量 value---直接赋值 输出变量 default-- ...