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.

转载于:https://www.cnblogs.com/zzy0471/p/6897197.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值