zoukankan      html  css  js  c++  java
  • 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

    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();
    }
    

    图片.png-2.4kB

    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();
    

    图片.png-3.1kB

    I think this tructure is hard to understand, but it's useful.

    That's all.

  • 相关阅读:
    P3469 [POI2008]BLO-Blockade
    洛谷P2342 叠积木
    洛谷 P1197 [JSOI2008]星球大战
    洛谷P1967 货车运输
    洛谷P2812校园网络【Network of Schools加强版】
    洛谷P3003 苹果交货Apple Delivery
    luogu Eat the Trees
    插头DP模板
    [NOIP2017] 宝藏
    LOJ6268拆分数
  • 原文地址:https://www.cnblogs.com/zzy0471/p/6897197.html
Copyright © 2011-2022 走看看