轻量ORM-SqlRepoEx (六) JOIN

示例使用的是最新 SqlRepoEx 2.0.2

可在:https://github.com/AzThinker/SqlRepoEx2.0Demo

或:https://gitee.com/azthinker/SqlRepoEx2.0Demo

演示模块:GettingStartedNorthwind

 

1、新建一个控制台程序;

2、通过Nuget包管理,下载SqlRepoEx.MsSql.Static

3、根据Northwind数据库的 Orders、Customers、Employees三张表结构生成三个简单的同名类;

4、 创建初始方法,初始一个工厂类。

 1 /// <summary>
 2         /// init
 3         /// 创建初始方法,初始一个工厂类。
 4         /// </summary>
 5         static void Init()
 6         {
 7             // Set Connection String
 8             string ConnectionString = "Data Source=(Local);Initial Catalog=Northwind;User ID=test;Password=test";
 9             var connectionProvider = new ConnectionStringConnectionProvider(ConnectionString);
10             MsSqlRepoFactory.UseConnectionProvider(connectionProvider);
11 
12             // this Demo is POJO ,So Using SimpleWritablePropertyMatcher()。
13             // 本例中,使用的是简单类,所以用SimpleWritablePropertyMatcher()来操作属性。
14             MsSqlRepoFactory.UseWritablePropertyMatcher(new SimpleWritablePropertyMatcher());
15         }

5、为了演示JOIN Orders类增加几个相关字段

1)、关联Customers

  public string CompanyName { get; set; }

 2)、  关联Employees

  public string LastName { get; set; }

  public string FirstName { get; set; }

 6、实例一个Orders仓储

    var repoCustomers = MsSqlRepoFactory.Create<Orders>();

 7、使用 SqlRepoEx 建立一个联接查询

1 var cust = repoCustomers.Query().Select(c => c.OrderID, c => c.CompanyName, c => c.FirstName, c => c.LastName, c => c.OrderDate)
2                                   .InnerJoin<Customers>()
3                                   .On<Customers>((r, l) => r.CustomerID == l.CustomerID, l => l.CompanyName)
4                                   .InnerJoin<Employees>()
5                                   .On<Employees>((k, q) => k.EmployeeID == q.EmployeeID, q => q.FirstName, q => q.LastName)
6                                   .Top(10);

8、此查询的实际 SQL 语句

 

Console.WriteLine(cust.Sql());

...

 1 SELECT TOP (10) [dbo].[Orders].[OrderID]
 2     , [dbo].[Customers].[CompanyName]
 3     , [dbo].[Employees].[FirstName]
 4     , [dbo].[Employees].[LastName]
 5     , [dbo].[Orders].[OrderDate]
 6 FROM [dbo].[Orders]
 7     INNER JOIN [dbo].[Customers]
 8     ON [dbo].[Orders].[CustomerID] = [dbo].[Customers].[CustomerID]
 9     INNER JOIN [dbo].[Employees]
10     ON [dbo].[Orders].[EmployeeID] = [dbo].[Employees].[EmployeeID];

 

 

 

9、查询结果

 

 1 /// <summary>
 2         /// Join 演示
 3         /// </summary>
 4         public static void DoJoin()
 5         {
 6             var repoCustomers = MsSqlRepoFactory.Create<Orders>();
 7 
 8 
 9             var cust = repoCustomers.Query().Select(c => c.OrderID, c => c.CompanyName, c => c.FirstName, c => c.LastName, c => c.OrderDate)
10                                   .InnerJoin<Customers>()
11                                   .On<Customers>((r, l) => r.CustomerID == l.CustomerID, l => l.CompanyName)
12                                   .InnerJoin<Employees>()
13                                   .On<Employees>((k, q) => k.EmployeeID == q.EmployeeID, q => q.FirstName, q => q.LastName)
14                                   .Top(10);
15 
16             Console.WriteLine(cust.Sql());
17 
18             foreach(var item in cust.Go())
19             {
20                 Console.WriteLine($"{item.OrderID}\t{item.CompanyName}\t{item.FirstName}\t{item.LastName}\t{item.OrderDate};");
21             }
22 
23         }

 

 1 10258   Ernst Handel    Nancy   Davolio 1996-07-17 0:00:00;
 2 10270   Wartian Herkku  Nancy   Davolio 1996-08-01 0:00:00;
 3 10275   Magazzini Alimentari Riuniti    Nancy   Davolio 1996-08-07 0:00:00;
 4 10285   QUICK-Stop      Nancy   Davolio 1996-08-20 0:00:00;
 5 10292   Tradi??o Hipermercados  Nancy   Davolio 1996-08-28 0:00:00;
 6 10293   Tortuga Restaurante     Nancy   Davolio 1996-08-29 0:00:00;
 7 10304   Tortuga Restaurante     Nancy   Davolio 1996-09-12 0:00:00;
 8 10306   Romero y tomillo        Nancy   Davolio 1996-09-16 0:00:00;
 9 10311   Du monde entier Nancy   Davolio 1996-09-20 0:00:00;
10 10314   Rattlesnake Canyon Grocery      Nancy   Davolio 1996-09-25 0:00:00;

 

posted @ 2018-10-06 09:23  Tikyang  阅读(476)  评论(0编辑  收藏  举报