Light ORM sqlrepoex (6) JOIN

Keywords: C# SQL github Database

The example uses the latest SqlRepoEx 2.0.2

Available at: https://github.com/AzThinker/SqlRepoEx2.0Demo

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

Demo module: GettingStartedNorthwind

 

1. Create a new console program;

2. Download SqlRepoEx.MsSql.Static through Nuget package management

3. According to the Orders, Customers and Employees table structure of Northwind database, three simple classes with the same name are generated;

4. Create an initial method to start a factory class.

 1 /// <summary>
 2         /// init
 3         /// Create an initial method to start a factory class.
 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             // In this case, a simple class is used, so the SimpleWritablePropertyMatcher()To manipulate properties.
14             MsSqlRepoFactory.UseWritablePropertyMatcher(new SimpleWritablePropertyMatcher());
15         }

5. To demonstrate that JOIN adds several related fields to the Orders class

(1) , associate Customers

  public string CompanyName { get; set; }

(2) associate Employees

  public string LastName { get; set; }

  public string FirstName { get; set; }

6. Instance an Orders warehouse

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

7. Use SqlRepoEx to create a join query

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. The actual SQL statement for this query

 

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. Query results

 

 1 /// <summary>
 2         /// Join Demonstration
 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 by Misery_Kitty on Fri, 20 Dec 2019 09:49:10 -0800